sasori sasori - 6 months ago 29
jQuery Question

how to check if variable value is one of the options in select2 when it opens?

how to detect if my variable value does exist in one of the options inside the select2 ?
e.g I want to remove that option from the selections if it does exist once the select2 opens.

so far, I only have a condition to detect if the select2 is open, so how to check if my var does exist inside the options?

var myvariable = $('form#myform #toremove').val();
$('form#myform .select2').on('select2:open', function(e){
//this part is wrong
$('form#myform .select2 option').each(function(){
if($(this).val() == myvariable){


Remove it directly after opening

$('.select2 option[value="'+myvariable+'"]').remove()