1

I need to trigger the submit or "Save" button for a widget when the widget is added to a specific sidebar in the backend. The reason for this is I am setting a certain taxonomy for a Dropdown Menu inside each widget. The code for setting the taxonomy works fine as long as after adding a widget you hit the "Save" button or refresh the page after the widget has been added.

Here is my code inside jQuery I am using to capture the Ajax event when a widget gets added to a sidebar and to trigger the submit button to force a Save action.

$( document ).ajaxStop( function() {
    var $saveBtns = $('#products_sidebar').find('input[submit]').trigger('submit');
} ); 

I am capturing the Ajax event correctly and able to console out the contents of the sidebar:

console.log("sidebar: " + $('#products_sidebar').html() );

However the submit action is not getting triggered.

thanks

1

Based off the example @mrwweb provided above and some additional code, here is an answer that works. This answer forces a widget to save when a widget is added to a sidebar. It finds all the "Save" buttons and triggers their click event thus saving the widget. The count variable is used to stop an endless loop from occurring.

var count = 0;
$( document ).ajaxStop( function() {
    var $saveBtns = $('#products_sidebar, #blog_sidebar').find('.widget-control-save');
    if (count < $saveBtns.length) {
        $saveBtns.each( function( index, value ){
            $(value).trigger('click');
            count++;
        });
    }
    else {
        count = 0;
        return;
    }

} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.