Skip to content Skip to sidebar Skip to footer

Clicking On Newly Added Links Don't Execute Javascript Function

while experimenting with jquery, I created this html and javascript.

Solution 1:

Use .on if you are using jQuery 1.7 or .delegate if you are using jQuery 1.6

jQuery('#results').on('click','.mylinkclass',function() {
    getLinkText(this);
});

Demo

Another way you can do this is you clone your existing element, Demo

var cloned = jQuery('.mylinkclass:last').clone(true);
jQuery('#results').append(cloned);

In the second case event listener will also be copied and you dont need to use .on or .delegate.

Solution 2:

You need an delegate event handler, because you items are created dynamically after dom load.

$('#results').on('click', '.mylinkclass', function() {
    getLinkText(this);
});

Solution 3:

When you add a link to the DOM after loading the page the first time, the links are not bound to the jQuery event. You should use:

$(document).on('click', '.mylinkclass', function() {

});

I was using "live()" when I first ran into this problem, but you shouldn't use that because it's deprecated according to the jQuery documentation.

Solution 4:

You have to do it this way:

$ ('. mylinkclass'). live ('click', function () {
alert ('OK')
});

Post a Comment for "Clicking On Newly Added Links Don't Execute Javascript Function"