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

JavascriptJquery

Javascript Problem Overview


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

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

Javascript Solutions


Solution 1 - Javascript

$('[name="ElementNameHere"]').doStuff();

jQuery supports CSS3 style selectors, plus some more.

See more

Solution 2 - Javascript

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

Solution 3 - Javascript

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)

Solution 4 - Javascript

jQuery("[name='test']") 

Although you should avoid it and if possible select by ID (e.g. #myId) as this has better performance because it invokes the native getElementById.

Solution 5 - Javascript

You could always do $('input[name="somename"]')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMSajjadiView Question on Stackoverflow
Solution 1 - JavascriptMadara's GhostView Answer on Stackoverflow
Solution 2 - JavascriptsteveukxView Answer on Stackoverflow
Solution 3 - JavascriptJoeri MinnekeerView Answer on Stackoverflow
Solution 4 - JavascriptSimon EdströmView Answer on Stackoverflow
Solution 5 - JavascriptMoo-JuiceView Answer on Stackoverflow