I use this jquery function to populate a dropdown box. Works great, but I want
to add an option:
<option value="">select</option>
the first option! How is this to be done?
function populate_cantons() {
$.getJSON('http://domain.com/v3/ajax/fetch_canton.php', {country:$('#country').val()}, function(data) {
var select = $('#cantons');
var options = select.prop('options');
$('option', select).remove();
$.each(data, function(index, array) {
options[options.length] = new Option(array['canton']);
});
});
}
http://jsfiddle.net/RhhAZ/1/
Note, I’m using
.next()to show it works. It will select the firstoptionwithout it. Usually, the firstoptionin aselectwill be selected if no other one is.EDIT
http://jsfiddle.net/RhhAZ/4/