Your code is fine. Here's a demo : http://jsfiddle.net/hKktc/1/
The reason you're not getting a value for the second alert call is because the attribute prefix
doesn't exist for the select
and only exists for the option
The 2nd alert
will return null, because <select>
has no attribute prefix
.
What is it you are expecting exactly?
I find that the second alert - alert( $(this).attr('prefix') );
is the one causing a problem.
As is, you get an alert of the prefix number, then an alert of null (caused by the second alert).
In your code, $(this)
refers to the <select>
, not the option. The prefix
attribute does not exist on the <select>
This will cause the problem with the second example.
You probably wanted .val()
not .attr('prefix')
, to obtain selected value of <select>
.
$(document).ready(function() {
$('#Nazione').change(function(){
alert( $(this).find("option:selected").attr('prefix') );
alert( $(this).val('prefix') );
});
});