How do I rebind event after widget save

I am writing a widget who’s form elements have a click event attached to. I bind it using jQuery.

However, after clicking save, the bindings gets lost. That is, clicking those elements doesn’t trigger the function anymore.

I tried using the on() jQuery method, as suggested here, but that doesn’t help:

    var buttonsHolder=jQuery('#widgets-right .icon_buttons');            
    // save the array of big icons, for later reference, and assign them the click event
    var  buttons=buttonsHolder.children('input');
    buttons.on("click",function(){
        openPanel(jQuery(this),jQuery(this).index());
    });

I could put my js code inside the widget form, as suggested here. That does solve the problem, but I really prefer to keep my js code separated.

What do I need to do in order to keep the click event of those elements working even after the save button is clicked?

2 Answers
2

WordPress provides callbacks for widgets interactions. You’re seems interested in two of them:

widget-added, widget-updated

Usage example:

jQuery(document).on('widget-updated', function(e, widget){
    // do your awesome stuff here
    // "widget" represents jQuery object of the affected widget's DOM element
});

Leave a Comment