How can I get name of element with jQuery?

134

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

This question is tagged with jquery html

~ Asked on 2012-01-13 15:11:40

The Best Answer is


234

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

~ Answered on 2012-01-13 15:13:01


14

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.

~ Answered on 2012-01-13 15:13:55


Most Viewed Questions: