Search code examples
javascriptjquery-select2-4jquery-select2

How to set select2 to a specific value?


I have a website. There you can either go manually to a specific site, or go there by using a dropdown menu (select2). If I go there manually (link), I want to change the value in the dropdown to the sites one.

Here is my select2 code:

$(function () {
  $("#siteSelect").select2().on("select2:select", function (e) {
   $("#siteSelect").val(-1).trigger("change");
        var id = $(this).val();
        var url = siteRoot + "/site/site?siteID=" + id ;
        $("#Container").load(url);
    });
});

Can you give me some hints how to do that?

I tried to set the value by: $("#siteSelect").val(siteName);

Every help is appreciated!


Solution

  • $("#siteSelect").select2().select2('val','your value');