0

I have converted some of my selects in my website to select2's and I notice that the following code no longer works. Assume I have in my html:

<select id = "CountryList" > </select>

Then

$("#CountryList").select2();

function Func() {
document.getElementById("CountryList").value = "";
}

Func();

What is the simplest way to fix this.

Edit: I am trying to set the selected value of the select to the empty string.

Edit 2: For anyone needing this as a function in the future I have added an answer

IntegrateThis
  • 853
  • 2
  • 16
  • 39

2 Answers2

0

If you are trying to empty the selection, use: $("#CountryList").select2("val", "");

EDIT: If you simply want to add a empty string and select it: $("#CountryList").append('<option value="" selected="selected"></option>')

Mathieu
  • 151
  • 1
  • 6
0

This works:

function EmptyValueForSelect2(SelectId) {
$("#"+SelectId).val("").trigger("change");


}

function SetValueForSelect2(SelectId, Value) {
$("#"+SelectId).val(Value).trigger("change");

}
IntegrateThis
  • 853
  • 2
  • 16
  • 39