Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get name of element with jQuery?

Tags:

html

jquery

People also ask

How do I get the name of an element in HTML?

JavaScript – Tag Name of an HTML Element To get the tag name of a specific HTML Element as a string, using JavaScript, get reference to this HTML element, and read the tagName property of this HTML Element. tagName is a read only property that returns the tag name of this HTML Element as a string.

What is the syntax to select an element with name?

The name attribute selector can be used to select an element by its name. This selector selects elements that have the value exactly equal to the specified value.

How can I get the ID of an element using jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


You should use attr('name') like this

 $('#yourid').attr('name')

you should use an id selector, if you use a class selector you encounter problems because a collection is returned


To read a property of an object you use .propertyName or ["propertyName"] notation.

This is no different for elements.

var name = $('#item')[0].name;
var name = $('#item')[0]["name"];

If you specifically want to use jQuery methods, then you'd use the .prop() method.

var name = $('#item').prop('name');

Please note that attributes and properties are not necessarily the same.


$('someSelectorForTheElement').attr('name');

Play around with this jsFiddle example:

HTML:

<p id="foo" name="bar">Hello, world!</p>

jQuery:

$(function() {
    var name = $('#foo').attr('name');

    alert(name);
    console.log(name);
});

This uses jQuery's .attr() method to get value for the first element in the matched set.

While not specifically jQuery, the result is shown as an alert prompt and written to the browser's console.


var name = $('#myElement').attr('name');