Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selectors: targeting inputs in a form

I have the following:

$('#registration_form input[type=text], #registration_form input[type=password]')

And I feel it can be much better: faster and shorter

Something like

$('#registration_form input[type=text,password]')

EDIT: THIS is what I really wanted! Please take a look here

like image 885
Dan Avatar asked Jan 16 '26 22:01

Dan


1 Answers

You can use find() with the :text and :password selectors to shorten your syntax:

$("#registration_form").find("input:text, input:password")

From a performance standpoint, it might be slightly faster, but not by much.

EDIT: Since :text and :password are not native CSS selectors, the following variation using filter() might be faster (and it's shorter in the first place):

$("#registration_form input").filter(":text, :password")
like image 180
Frédéric Hamidi Avatar answered Jan 19 '26 14:01

Frédéric Hamidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!