How do I get the texbox id when the focus is on

1

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?

    
asked by Romer Ariel Romero Isnado 07.05.2018 в 20:37
source

2 answers

0

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!

    
answered by 07.05.2018 в 20:54
0

Using% pure% co_, you can use this in the function where you work:

var focusedElement = document.activeElement;
console.log(focusedElement.id);
    
answered by 07.05.2018 в 20:56