The problem is that I want to run an AJAX with the variable id_company, ONLY when an option of a multiselect is deselected, but I really do not know how to do it. Everything works fine until the condition is out of the event, that's where my AJAX should be. Here is my code below:
$('#select_empresas').on('change',function(e) {
//if($(this).prop("selected",false)){
var id_usuario = <?=$userID?>;
var id_empresa = 0;
$(this).find("option").each(function(i, v) {
if (!$(v).is(":selected") && $.inArray($(v).val(), empresas) >= 0) {
id_empresa = $(v).val();
var i = empresas.indexOf($(v).val());
if(i != -1) {
empresas.splice(i, 1);
}
} else if ($(v).is(":selected") && $.inArray($(v).val(), empresas) < 0) {
empresas.push($(v).val());
id_empresa = $(v).val();
}
});
//if an option is deselected, execute the code below
if($(this).find('option').filter(':selected:last').prop("checked")==false){
console.log(id_empresa);
/*******
Code...
********/
}
});
The problem is that I'm using the Materialize framework, and it generates a multi-select
like this:
I hope and you can help me, thanks.