Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change select2 option on click

I am using the select2 plugin on my select services_dd in my form, I have a link on the page with a class .js-autoselect. On clicking this I want to take the ID of that link tag and change the select2 option depending on the button they clicked.

The <option> value attribute matches the attribute of the id on the link. The code below is successfully changing the value but the select2 is not updating, it still shows the first selection.

$('.js-autoselect').click(function(e){
    e.preventDefault();
    var option = $(e.target).attr('id').trim().toLowerCase(); // option =link1

    $('#services_dd option:selected').val(option);
    alert($('#services_dd option:selected').val()); // this alerts link1 which is correct

});

Does anyone know how to update the select2

like image 420
CR41G14 Avatar asked Dec 21 '12 09:12

CR41G14


1 Answers

$("#services_dd").select2("val", option);

See the Programmatic Access section of the documentation: http://ivaynberg.github.com/select2/#documentation

like image 80
scoota269 Avatar answered Nov 13 '22 01:11

scoota269