Reputation: 41
I have a country select with the option tag containing a custom attribute : name.
I want to assign the value of this attribute to a javascript variable which would then be appended to a link and sent forward.
But with the current code I am unable to set the value of the variable.
Following is the line of code that I have currently:
var code = $("#countrySelect option:selected", this).attr("name");
<select name="CountrySelector" id="countrySelect">
<option selected="selected" value="0">Select</option>
<option value="355" name="al">Albania</option>
<option value="213" name="dz">Algeria</option>
</select>
I need to assign the value of the custom attribute "name" to the variable code.
Can anyone please help me in resolving this?
Upvotes: 0
Views: 3628
Reputation: 40554
Remove the this
from your code:
$("#countrySelect option:selected").attr("name");
But you should really be using data-*
attributes to store random data in your HTML elements. jQuery makes them easy to use them: http://api.jquery.com/data/
Upvotes: 1