views:

44

answers:

3

Hello, all!

I have various inputs, selects and textareas in a form. I'd like to select one of them based on name without knowing the type of the element with jQuery. I know I can do something like

$("input[name=foo], textarea[name=foo], select[name=foo]")

but I wonder if I can do it in a more concise way. What do you say?

+1  A: 
$(':input[name=foo]')

Notice the colon just before the word input. In jQuery, this selects all inputs, textareas, selects and buttons that have an attribute name="foo".

Documentation

BoltClock
+7  A: 

http://api.jquery.com/input-selector/

:input[name=foo]

Anpher
+1  A: 

You can use the :input filter selector:

$(':input[name="name here"]')

Description: Selects all input, textarea, select and button elements.

The :input selector basically selects all form controls.

More Info: http://api.jquery.com/input-selector/

Sarfraz