T1000
T1000

Reputation: 2941

Make jQuery works for element created with "insertAfter" etc

I have some elements like <a class='link'>click</a> which are created after clicking on another div ...
Is there any chance to make jQuery works for <a class='link'>click</a> ?

For now I'm forced to use <a onclick='func()' class='link'>click</a> ... and it's not possible to make something like alert($(this).html()) (which must pop-up the text "click").

Thanks

Upvotes: 1

Views: 654

Answers (1)

user113716
user113716

Reputation: 322502

You could use jQuery's live() method which will handle events for dynamically added elements.

$('a.link').live('click', function() {
    alert( $(this).html() );
});

Or you could just bind the click like normal when you create them.

$('<a class="link">click</a>').click(function() {
    alert( $(this).html() );
}).insertAfter(someselector);

Upvotes: 4

Related Questions