HI how to remove empty value in multi select, bellow my code
$(document).on('blur','.select2-search__field', function(e) {
var current_select = window.localStorage.getItem("current_select");
console.log($(this).val());
if (current_select) {
if ($(this).val()) {
$("#"+current_select).append('<option value="'+$(this).val()+'" selected>'+$(this).val()+'</option>');
//$("#"+current_select).val($(this).val());
$("#"+current_select).trigger("change");
}
}
});
when I enter new values that value appended it is working , but same time one empty value automatically comes so how can i removed please help me
Output is :
<option value=" " selected="selected"></option>
<option value=" 12345" selected="selected"> 12345</option>
<option value=" abcd" selected="selected">abcd </option>
First value is empty, so how can I remove this.
try this
$('select option')
.filter(function() {
return !this.value || $.trim(this.value).length == 0;
})
.remove();
or
$("#select option[value=' ']").remove();