Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: select first-child input inside form but not when type:hidden?

I have a variable that hold different form-objects like this … $(formId)

Each form is different as in either has an hidden input field as first element or the first-child is a normal input (type="text" or something else)

how can I ALWAYS select the the first "normal" input field

$(formId).find("input:first-child:not[type='hidden']")

I thought it should be something like this. So if you wonder what I need this for - I want to set the focus to the first input field in the form, but if the first input is a hidden one I of course want to set it to the next "visible" input.

like image 676
matt Avatar asked Feb 18 '12 15:02

matt


2 Answers

The :first-child selector selects an element which is the first child, not the first item in a jQuery collection.
To select the first element of a collection, use jQuery's :first selector.

Your implementation of :not is also wrong.
:not[type='hidden'] is equal to :not()[type='hidden'] is equal to [type='hidden'] - The opposite of what you want.


These are the right selectors (they're equivalent):
input:not([type=hidden]):first
input[type!=hidden]:first
like image 167
Rob W Avatar answered Oct 21 '22 22:10

Rob W


$(formId).find ('input:visible:first').focus();

like image 20
David Thomas Avatar answered Oct 21 '22 21:10

David Thomas