I am carrying out a validation to many text fields, these are dynamic. My question is how can I know the name of my text box with the focus method, something like this.
Document.getElementById("focus...")
Could it be possible?
I am carrying out a validation to many text fields, these are dynamic. My question is how can I know the name of my text box with the focus method, something like this.
Document.getElementById("focus...")
Could it be possible?
If possible, it is done as follows;
// Obtenes el elemento que está en foco:
var $focused = $(':focus');
// Lo mismo pero sin jQuery:
var focused = document.activeElement;
// Retorna un booleano, true, si el elemento está en foco, y false en caso contrario:
var hasFocus = $('foo').is(':focus');
// Lo mismo pero sin jQuery:
elem === elem.ownerDocument.activeElement;
I clarify; I based my response on gdoron at StackOverflow and English
Then, in your particular case, to get the name, you could do something like that
$(':focus').attr('name')
Greetings!
Using% pure% co_, you can use this in the function where you work:
var focusedElement = document.activeElement;
console.log(focusedElement.id);