38

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?

4 Answers 4

78
$(':input:enabled:visible:first').focus(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Funny story, I was waiting for the time to elapse so I could select it as the answer, and the power in the office went out.
I am using a boostrap modal popup and can't get this to work if it is a dropdownlist. Only if it is a textbox, I can use the focus() in codebehind. Any idea why bootstrap modal would not work?
12

As an extension of Jordan's answer

$(':input:enabled:visible:not([readonly]):first').focus(); 

This one also excludes readonly inputs

Comments

5

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!

Comments

4

Try this:

$("input[type='text']:enabled","another selector","another selector").first().focus(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.