How do I select an element with its name attribute in jQuery?

05/05/2020 22:00:01

How to get an element with its name attribute with jQuery?

Is there anything (like # for id and . for class) for name in jQuery?

Verified Answer (573 Votes)

03/13/2012 12:39:35
$('[name="ElementNameHere"]').doStuff();

jQuery supports CSS3 style selectors, plus some more.

See more

573

Answer #2 (36 Votes)

03/13/2012 12:41:13

You can use:

jQuery('[name="' + nameAttributeValue + '"]');

this will be an inefficient way to select elements though, so it would be best to also use the tag name or restrict the search to a specific element:

jQuery('div[name="' + nameAttributeValue + '"]'); // with tag name
jQuery('div[name="' + nameAttributeValue + '"]',
     document.getElementById('searcharea'));      // with a search base
36

Answer #3 (28 Votes)

03/13/2012 12:42:19

it's very simple getting a name:

$('[name=elementname]');

Resource:

http://www.electrictoolbox.com/jquery-form-elements-by-name/ (google search: get element by name jQuery - first result)

28
3
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.