It's fairly straightforward. I want to set focus to the first enabled and not hidden control on the page.
For a textbox, I have
$("input[type='text']:visible:enabled:first").focus();
But I want to get "all" form input controls: textbox, checkbox, textarea, dropdown, radio in my selector to grab the first enabled and not hidden control. Any suggestions?
To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.
The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.
Using jQuery With jQuery, you can use the . focus() method to trigger the “focus” JavaScript event on an element. This method is a shortcut for . trigger("focus") method.
In jQuery by using blur() property we can remove focus from input textbox field.
$(':input:enabled:visible:first').focus();
As an extension of Jordan's answer
$(':input:enabled:visible:not([readonly]):first').focus();
This one also excludes readonly inputs
This is a little more comprehensive:
$('form :input:text:visible:not(input[class*=filter]):first').focus();
Lamen: Focus the cursor in the first text input in a form that is visible, but if it use the class named "filter", ignore it!
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