Bootstrap-select Add Item And Select It


Answer :

You have a typo. Instead of:

$('#myselect').append('<option val="'+newitemnum+'">'+newitemdesc+'</option>'); 

You need:

$('#myselect').append('<option value="'+newitemnum+'">'+newitemdesc+'</option>'); 

Here is a JSFiddle demo: http://jsfiddle.net/xbr5agqt/

The "Add and select 'Soy Sauce' option" button does the following:

$("#myselect").append('<option value="'+newitemnum+'">'+newitemdesc+'</option>'); $("#myselect").val(4); $("#myselect").selectpicker("refresh"); 

One slightly faster approach (used by the "Add and select 'Relish' option" button) is to append the new <option> element with the selected attribute already applied:

$("#myselect").append('<option value="'+newitemnum+'" selected="">'+newitemdesc+'</option>'); $("#myselect").selectpicker("refresh"); 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable