4

I am trying to detect changes on a JQuery MultiSelect to trigger an update in another place. The normal javascript on change stuff does not work because the select does not change. I found a method called “beforeclose” that is called from the close function of the MultiSelect. I think I can use that. I found that if I add it when making the MultiSelect it works as expected and logs the message when closing the select. See bellow:

$j("#selectId").multiselect({
    checkAllText: 'Select all',
    uncheckAllText: 'Deselect all',
    beforeclose : function() {
        console.log("before close")
    }
}).multiselectfilter();

But I can not do this because the MultiSelect is created some other place, so I need to just add that method to it. I tried lots of different things like:

$j("#selectId").multiselect().data("ech-multiselect").beforeclose = function() {
    console.log("before close")
};

But nothing seems to work. Any ideas? Thanks

user1404617
  • 585
  • 1
  • 5
  • 20

1 Answers1

1

Can you create a codepen or jsfiddle to replicate the issue you're having? I'm sure I or someone else could assist with a working model.

I think your syntax is a little off if you want to use beforeclose. If it were a dialog, you'd use

$( ".selector" ).dialog({
  beforeClose: function( event, ui ) {}
});
// and bind an event listener
$( ".selector" ).on( "dialogbeforeclose", function( event, ui ) {} );

Alternatively, you could use jQuery's on function. On change would look like:

$(document).on('change', '#selectId', function() {
  // Fires every time selectId is changed
});

https://api.jquery.com/on/#example-7 a custom event handler could work for you.

Caleb Davenport
  • 613
  • 5
  • 18