Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"on" not binding to dynamically added elements

Tags:

jquery

My HTML

<div>    
    <span  class="more-available" data-completeMessage="This is the complete message you see after clicking more">Hello</span>​
</div>

I add a anchor tag to the end dynamically and then want to attach a click handler to the anchor tag. So I do this

$(document).ready(function() {

   //Attach future proof click event to an anchor tag
   $('a.more').on('click', function() {
      var $parent = $(this).parent();
      $parent.text($parent.data('completemessage'));
   });

   //Add the anchor tag
   $('span.more-available').append($('<a class="more">...more</a>'));
});;​

This does not work. If i replace "on" by "live" it works. (but live is depreciated)

I know I can do this

$(document).ready(function() {

    $('div').on('click','a.more', function() {
        var $parent = $(this).parent();
        $parent.text($parent.data('completemessage'));
    });

    $('span.more-available').append($('<a class="more">...more</a>'));
});;​

and it works, but my question is...

Was I wrong in assuming that "on" provides all the functionality of live? Does "on" not bind to future elements? Is this correct behavior, or am I doing something wrong.

fiddle: http://jsfiddle.net/arishlabroo/pRBke/5/

like image 576
labroo Avatar asked Apr 27 '12 17:04

labroo


People also ask

How do you bind a click event to dynamically created elements?

To bind the event handler to dynamically created elements, we will be using Event Delegation. On clicking the new list items, the same action is performed.

Which one of the given option is used to add new element dynamically in html5?

New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method.

How do you dynamically assign an event listener to a button using JavaScript?

This is a lot simpler than you think, in our function that will create our new element, we need to attach the event handler, and function we want to assign to it, this can be done like so: // Create the new element var li = document. createElement('li'); li. className = 'dynamic-link'; // Class name li.

Which jQuery event listener can you use to bind the value in an input field to a variable?

Use jQuery . submit() to modify the value at the time of form submission (value is modified before submit, then submit continues).


2 Answers

on() is just a binder that allows for target delegation. It's more of a replacement for delegate() than for live().

$('foo').live('click',fn); is essentially $(document).on('click','foo',fn);

With that in mind, you simply bind the click event to the constant parent wrapper and delegate to your target, like so:

$('span.more-available').on('click', 'a.more', function(){
    var $parent = $(this).parent();
    $parent.text($parent.data('completemessage'));
});
like image 90
AlienWebguy Avatar answered Oct 25 '22 21:10

AlienWebguy


Use it like below,

$('span.more-available').on('click', 'a.more', function () {
   //..your code
});

You cannot just replace replace .live with .on instead you need to bind the handler to the parent element with the specific selector meaning the handle will be delegated when the event is triggered for the matching selector. In above, the you are adding a listener to span.more-available which will execute the handler only when the matching selector a.more is triggered.

In short follow the two steps to replace .live with .on,

  1. Find the closest parent elements to which the element will be added dynamically.
  2. Bind the handler to parent element with the dynamic elements selector as the 2nd argument.
like image 39
Selvakumar Arumugam Avatar answered Oct 25 '22 22:10

Selvakumar Arumugam