sahar
sahar

Reputation: 569

how to get elements by name using jquery?

how to get html element by name using jquery without using id or class ? Is there any way to do that?

Upvotes: 11

Views: 69511

Answers (9)

ShangranLab
ShangranLab

Reputation: 11

May be this:$('div[name="element_name"]').

Or other element type like:

img: $('img[name="element_name"]')

td: $('td[name="element_name"]') ...

Upvotes: 1

Manmeet Khurana
Manmeet Khurana

Reputation: 377

To get the value, we can use multiple attributes, one of them being the name attribute. E.g

$("input[name='nameOfElement']").val();

Upvotes: 1

Rick
Rick

Reputation: 1391

It should be known that the only correct answers that have been given are the ones that included quotes around the attribute value, ie.

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

It is a mandatory requirement to include quotes around the value, see: http://api.jquery.com/attribute-equals-selector/

If you are using "name" in reference to the nodeName or "tag", then simply select by that string:

$("div")
$("a")
$("p")
$("input")
$("body")

Upvotes: 23

Kris Krause
Kris Krause

Reputation: 7326

Not sure if you mean tag name or named attribute. So here is both -

Attribute Equals Selector [name="value"]

http://api.jquery.com/attribute-equals-selector/

or Element Selector (“element”)

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

// name attribute selector
$('[name=whatever]').each(function(index) {
  alert(index);
  // and of course depending on your element utilize $(this).doSomethingCool
});

// tag name selector for all h1 tags
$('h1').each(function(index) {
  alert(index);
  // and of course depending on your element utilize $(this).doSomethingCool
});

Upvotes: 4

emboss
emboss

Reputation: 39650

If you mean the name attribute, it's this:

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

If you mean the element name, then it's like this:

$('ul')

Upvotes: 4

jfriend00
jfriend00

Reputation: 708146

In jQuery, you can use lots of things besides just id and class. The CSS3 selector specification is full of other things you can use and jQuery even has some more things than this I believe. For example, you can use tag type, attribute values, position in the hierarchy. These are all valid jQuery selector objects:

$("div")
$("div span")
$("[name=nyName]");
$("div span [name=nyName]");
$("ul li [name=nyName]");

In many cases, you will get better performance with selectors based on tag types, id values and class names because many browsers support searching for those in native code.

Upvotes: 1

Rafay
Rafay

Reputation: 31043

get the element by its name like

$("div")

DEMO

Upvotes: 0

Joseph Silber
Joseph Silber

Reputation: 220136

Use this:

$('[name=thename]');

Note: For performance reasons, it's better to specify the tag name. So, if you're looking for images with a name of winter, you'll use this:

$('img[name=winter]');

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 191058

$('tagName')...

for <tagName />

or

$('[name="attributeName"]')...

for <input name="attributeName" />

The jQuery selector works like a CSS selector.

Upvotes: 1

Related Questions