Answers for "selected option select2"

0

select2 add option

// Set the value, creating a new option if necessary
if ($('#mySelect2').find("option[value='" + data.id + "']").length) {
    $('#mySelect2').val(data.id).trigger('change');
} else { 
    // Create a DOM Option and pre-select by default
    var newOption = new Option(data.text, data.id, true, true);
    // Append it to the select
    $('#mySelect2').append(newOption).trigger('change');
}
Posted by: Guest on November-06-2020
-2

select 2 select a value

SELECT2 V4 :
============
For select2 v4 you can append directly an option/s as follow:

<select id="myMultipleSelect2" multiple="" name="myMultipleSelect2[]">
    <option value="TheID" selected="selected">The text</option>                                                                   
</select>

Or with JQuery:
---------------

var $newOption = $("<option selected='selected'></option>").val("TheID")
					.text("The text")
 
$("#myMultipleSelect2").append($newOption).trigger('change');

other example :
---------------

$("#myMultipleSelect2").val(5).trigger('change');
Posted by: Guest on December-23-2020

Code answers related to "selected option select2"

Browse Popular Code Answers by Language