Reputation: 617
I want to get select box name in jquery.I already know this.name.But I need to get name based on id I have tried
<select name='country' id='select_id'>
<option .........</option>
</select>
I need to fetch the name country dynamically using id.I tried the below line
$("#select_id").name
But it is not working.
Upvotes: 0
Views: 61
Reputation: 15387
Use as
$("#select_id").prop('name');
jQuery 1.6 introduced the .prop() method for setting or getting properties on nodes and deprecated the use of .attr() to set properties. However, versions up to 1.9 continued to support using .attr() for specific situations. This behavior in the name of backwards compatibility causes confusion when selectors are used that distinguish between attributes and properties.
.attr() retrieves attributes, while .prop() retrieves properties. The two are different things, and always officially have been (though the DOM often has a property to correspond to an attribute). If you have a , where whatever is not an attribute the browser recognizes, you'd use .attr() to get the attribute's value. .prop() wouldn't even be able to see it in most browsers.
When you care about the resulting DOM property, use .prop(). When you care about the actual HTML attribute, use .attr(). It's not really an either/or thing; you can use both in the same code and the universe won't implode even once (assuming you've used them correctly, anyway). :) Just use the one that's suited to the job you're doing at the time, and quit trying to "deprecate" stuff that's not broken.
Upvotes: 0
Reputation: 82231
You need to use .attr() to get/set attribute name:
$("#select_id").attr('name');
Upvotes: 1