0

I am authoring a WP Widget plugin, and am having problems enqueuing javascript onto the Widget Admin page. The Javascript works when the page is first loaded or refreshed, but when you click "Save" on one of the Widgets, the page gets reloaded, MINUS the javascript?

I am enqueuing the external javascript file like this:

function enqueue_admin_scripts() {
  wp_register_script('mp3-admin-script', plugins_url('assets/js/wp-mp3-admin.js' , __FILE__ ), array( 'jquery' ));
  wp_enqueue_script('mp3-admin-script');
}
add_action('admin_enqueue_scripts','enqueue_admin_scripts');

As I said, everything works fine of that bat, but once the Widget Admin Form is submitted once, the page appears to reload and the javascript functions stop working, (no errors, it's as if the javascript no longer exists)!

Any ideas? Thanks for looking...

PS. Someone suggested it could be to do with Jquery's .on() "direct" binding, and that I should change all occurances for "event delegation" like so :

$('#wrapper').on('click', ".className", function() {...

I tried that, but it didn't seem to help...

  • Show us the code that is not working. Are you sure you did the new biding correctly? – aifrim Nov 3 '15 at 17:26
  • Yes, JQuery is a good solution. But please try $(document.body).on('submit', '#your-form', function(event) { – Trang Nov 4 '15 at 2:51

Your Answer

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

Browse other questions tagged or ask your own question.