Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a widget plugin that I've created, that lists about 10 fieldsets. jQuery runs at the beginning and hides and fieldsets that have no values. There are also buttons that allow to "Add" and "Remove" fieldsets.

Everything works great except for when its time to "Save". After clicking Save all of the jQuery stops working, all of the hidden fieldsets are shown, and the "Add" and "Remove" buttons no longer work. The only way to continue using the widget properly is to refresh the page

Anyone idea what might be causing this, or a possible solution?

share|improve this question
    
Is "Save" triggering an error? If you're using Firefox, you can check for these errors by installing Firebug and viewing the console. This will tell you a) if there's an error and b) what's causing the error. –  EAMann Dec 3 '10 at 18:51
add comment

3 Answers 3

You're looking for jQuery.live(), or jQuery's livequery plugin, depending on what you're seeking to do. (The second is the proper one if you need to use onChange events.)

As things stand, your jQuery hooks are not called when the save button is called, because the form's tags are reloaded using ajax, and jQuery(document).ready() is, obviously, not getting called as they are.

share|improve this answer
add comment

You just need to rebind the controls inside the jQuery.ajax callback function's 'success'.

Binding triggers by .live() might be an alternate option - I personally do prefer .on() function.

Just use a DOM inspector the check the DOM event bindings.

share|improve this answer
add comment

This is an Ajax thing. One solution to make your script work again when you save the widget:

jQuery(document).ajaxSuccess(function(event, xhr, settings) {
    var widget_id_base = 'your-widget-id-base';
    if(settings.data.search('action=save-widget') != -1 && settings.data.search('id_base=' + widget_id_base) != -1) {

        // Do your stuff again here

    }
});
share|improve this answer
    
I was going crazy not being able to figure out why my jQuery events weren't going after I saved a widget. This answer seriously saved me so many hours of work. +1 –  Ian Apr 8 at 2:47
    
There's another solution for this issue that is really creative, and it's worth a try too: Question 1300840 –  Marcio Duarte Apr 8 at 12:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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