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
Related Q & A:
- How can I use a button to retrieve a phone 'number' from contacts?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How can I get a good deal at a luxury resort?Best solution by lastminute.com
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- How can I get a text alert when I get an email?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.