Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/jQuery detect if input is focused [duplicate]

People also ask

How do you know if input is focused?

To check if an input field has focus with JavaScript, we can use the document. activeElement property to get the element in focus. to add an input. to check if the input element is focused.

How do you check if a input is focused or not in JavaScript?

To detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the document object. const elem = document. activeElement; The activeElement returns the currently focused element in the document.

How do you know if a textbox has focused?

Answers. Now you can check the variable "txtFocus" whenever you need to see what textbox has focus, just like this... if(txtFocus) alert(txtFocus.id + ': ' + txtFocus. value); else alert('No textbox currently has focus!'


With pure javascript:

this === document.activeElement // where 'this' is a dom object

or with jquery's :focus pseudo selector.

$(this).is(':focus');

If you can use JQuery, then using the JQuery :focus selector will do the needful

$(this).is(':focus');