In my application I am using select2 plugin for drop-down with multiple select values. And now I want to add some static option to the select2 drop-down.
<select multiple="" class="test">
<option value="a">Illustrations</option>
<option value="b">Maps</option>
<option value="add">Add Status</option>
</select>
And I am applying multi select, now when I click on add status option I want to do some functionality and this option will not selected as like the option.
And my js code :
$('.test').select2();
$('.test').on("select2-selecting", function(e) {
alert($(".test").val());
});
But alert
is giving all the selected values a,b,add
. But I want to get the 'add'
option only.
Please suggest me how can I do this.
Thanks in advance.
Try it with this:
alert($(".test option:selected").val());
Could it be? Or do you need to handle an event at the value 'add'?
$('.test').select2();
$('.test').on("select2:select", function(e) {
if( $('.test').val()[0] === "add" ) {alert("add")}
//or search value add
if($(".test").val().indexOf("add") > -1) {alert("search add")}
});