Shermaine Chaingan Shermaine Chaingan - 3 months ago 17
jQuery Question

set empty on first value in combobox

How can I set the first value to

null
? I used this codes but it does not work? How will I set a placeholder on combobox? Thank you in advance.

<select class="ddlASN"></select>


var result = 'a,b,c,d';

$('.ddlASN').html($.map(result.split(','), function(item) {
return $('<option></option>').val('').html('')[0].outerHTML
return $('<option></option>').val(item).html(item)[0].outerHTML
}).join(''));

Answer

You can try this.

    var result = 'a,b,c,d';

// placeholder option
var dd_content = '<option>-- Select an option --</option>';

// add rest of the options
jQuery.map(result.split(','), function(item) {
  dd_content += '<option value="'+item+'">'+item+'</option>';
});

jQuery('.ddlASN').html(dd_content);

It will prevent from getting values repeated after the submition.

If it doesn't work then please do tell what actually happens after submitting the form. Are the new values getting assigned to the combobox or not...