Widget Admin – Form Submit Event?

I’m creating a widget, and cannot figure out how to get an event right before the person clicks “save” or presses the enter button on the widget admin panel. What would be the javascript code to get the event on the widget admins’ form submit?

Example:
enter image description here

Thanks!

3 Answers
3

You need to listen for the click event (there is no such thing as a pre/before click) and do some work right then or figure out if you want to allow the click to “go through” or not based on some validation for example.

jQuery(function($) {
    // We are binding to the body so that the code
    // will work for future elements added to the DOM
    $('body').on('click', '.widget-control-save', function(ev) {
        var my_validation = true;

        if ( my_validation ) {
            console.log('widget save!');
        } else {
            ev.preventDefault();
            ev.stopPropagation(); /* We are capturing the event so it won't bubble up. */
        }
    })
});

Leave a Comment