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.
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With