How can you select an element that has current focus?
There is no :focus
filter in jQuery, that is why we can use something like this:
$('input:focus').someFunction();
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
The focus() is an inbuilt method in jQuery which is used to focus on an element. The element get focused by the mouse click or by the tab-navigating button. Here selector is the selected element. Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus event occurs.
focus() method may not fail directly, as the method still exists. However, the expected behavior will not occur. This method is deprecated.
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.
alert($("*:focus").attr("id"));
I use jQuery.
It will alert id of element is focusing.
I hope it useful for you.
$(document.activeElement)
will return the currently focused element and is faster than using the pseudo selector :focus.
Source: http://api.jquery.com/focus-selector/
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