Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery selector input[type=text]')

I wrote a code that basically selects all input type=text element like this:

$('.sys input[type=text]').each(function () {} 

How do I change it to select input[type=text] or select?

like image 331
Jack Avatar asked May 18 '12 09:05

Jack


1 Answers

Using a normal css selector:

$('.sys input[type=text], .sys select').each(function() {...}) 

If you don't like the repetition:

$('.sys').find('input[type=text],select').each(function() {...}) 

Or more concisely, pass in the context argument:

$('input[type=text],select', '.sys').each(function() {...}) 

Note: Internally jQuery will convert the above to find() equivalent

http://api.jquery.com/jQuery/

Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').

I personally find the first alternative to be the most readable :), your take though

like image 135
Andreas Wong Avatar answered Oct 02 '22 11:10

Andreas Wong