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

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

This question already has an answer here:

How can I select an element by name with jQuery?

14 answers

How to get an element with its name attribute with jQuery?
Is there anything (like # for id and . for class) for name in jQuery?

Solutions/Answers:

Solution 1:

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

jQuery supports CSS3 style selectors, plus some more.

See more

Solution 2:

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:

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:

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:

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