user398341
user398341

Reputation: 6577

How to find form element by it's name

I'm trying to find the form element type (i.e. textarea, input etc.) by only having its 'name' attribute. Is there any way of achieving this with jQuery?

Upvotes: 0

Views: 220

Answers (3)

lonesomeday
lonesomeday

Reputation: 237865

Get the element by its name attribute:

$('[name="someName"]')

Then get the first element with [0]:

$('[name="someName"]')[0]

Then get the tag name with nodeName and normalise it with toLowerCase:

$('[name="someName"]')[0].nodeName.toLowerCase()

In jQuery 1.6, you can do this slightly more beautifully (if a little more slowly) by using prop:

$('[name="someName"]').prop('nodeName');

I don't think this will be normalised, so you'll need to do so yourself.

Upvotes: 1

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

Do this:

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

:input returns all input form elements (textara, radios, texts, selects, etc)

Hope this helps. Cheers

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

$("input[name='yourname']")

$("textarea[name='yourname_textarea']")

Upvotes: 0

Related Questions