Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind click and live click

Tags:

jquery

Why this code works (see code at jsfiddle)

$(document).ready(function() {
    var html = '<div><a href="javascript:">click me</a></div>';
    var $div = $(html);
    $div.find('a').bind('click', function() { //attention on bind
        alert('Hi');
    });
    $('#test').append($div);
});

but the same code with .bind('click' replaced with .live('click' is not working? Why?

Thank you.

like image 966
Kirzilla Avatar asked Feb 23 '23 02:02

Kirzilla


1 Answers

jQuery documentation says:

DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector.

So if you change $div.find('a').bind('click' to $('#test a').live('click' it would work.

like image 181
Igor Dymov Avatar answered Mar 03 '23 19:03

Igor Dymov