How to speed up a wordpress function with multiple loops?

WordPress Theme Development: How can I trigger a JavaScript function when a particular widget's save button is pressed in widgets.php in the admin area?

  • This almost works but the alert won't fire on the first click on the Save button. It does after the second click however. So I'm looking for a way that is more reliable. class My_Widget extends WP_Widget { function __construct() { parent::__construct( 'my_widget', 'My Widget', array( 'description' => __( 'This is my widget.', 'my-widget' ), ) ); } function form( $instance ) { $input_id = "test_my_widget_" . uniqid(); ?> <input type="hidden" id="test_my_widget" value="" /> <script type="text/javascript"> jQuery( "#<?php echo $input_id; ?>" ).closest( "form" ).find( 'input[type=submit]' ).click( function( event ) { alert( 'clicked' ); }); </script> <?php } }

  • Answer:

    Try .on("click", "a.foo", fn); vs. .click() which looks in the live DOM (widgets are added by Javascript and thus are not always in the original DOM present when the page loads). http://api.jquery.com/on/

Nick Ciske at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.