WordPress Widget jquery live() or on(), admin side

something disturbing me since a while, jQuery API says :

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

As I try to add some jquery stuff in my widget admin side ( form() action of widget class ), I was surprise to not be able to use on(), but live() to do the job.

Read More

So here is my question : Why and what am I misunderstanding ?

Regards, and of course, thx for your time.

ps I’m offcurse using >= 1.7 version.

Edit:

<?php
    class Test_Slider_Widget extends WP_Widget 
        { 
            public function form( $instance ) { 
                ?>
             <span id="test">Test</span>
             <script> 
             jQuery(document).ready(function($) {
                 /* $('#test').on('click', function { //doesn't work
                     alert('test'); 
                 }); */
                 $('#test').live('click', function { //work
                     alert('test');
                 });

             });
             </script>


 <?php }

EDIT 2: Ok, always use $this->get_field_id(‘id-text’) to have a unique ID, or id will be duplicated cause of widget duplicated code in wordpress admin.
Thanks to Xec for showing me the way 🙂

Related posts

Leave a Reply

1 comment

  1. The deprecated .live() method binds to document, and on each and every click will check to see if the #test selector matches before invoking the handler. If you want this behavior with .on() you can use the delegate syntax.

    Check out http://api.jquery.com/on/#direct-and-delegated-events for more detailed information.

    For a quick-fix you can mimic your .live() code with something like this:

    $(document).on('click', '#test', function () {
        alert('test');
    });
    

    For more efficient code you would replace document with a selector matching a static ancestor of #test

    Hope this helps!

    edit:

    From your “edit 2” I realize your issue was duplicated IDs, but i’ll leave this answer here for posterity.