How can I get name of element with jQuery?

JqueryHtml

Jquery Problem Overview


How can I get name property of HTML element with jQuery?

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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.

Solution 3 - Jquery

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

Solution 4 - Jquery

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.

Solution 5 - Jquery

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

Solution 6 - Jquery

The method .attr() allows getting attribute value of the first element in a jQuery object:

$('#myelement').attr('name');

Solution 7 - Jquery

If anyone is also looking for how to get the name of the HTML tag, you can use "tagName": $(this)[0].tagName

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
QuestionPoonam BhattView Question on Stackoverflow
Solution 1 - JqueryNicola PeluchettiView Answer on Stackoverflow
Solution 2 - Jqueryuser1106925View Answer on Stackoverflow
Solution 3 - JqueryPatriciaView Answer on Stackoverflow
Solution 4 - JqueryrjbView Answer on Stackoverflow
Solution 5 - JqueryDennis TraubView Answer on Stackoverflow
Solution 6 - JqueryDidier GhysView Answer on Stackoverflow
Solution 7 - JqueryDoug NintzelView Answer on Stackoverflow