80

The jQuery click function works fine here

<div id="LangTable"><a class="deletelanguage">delete</a></div>    

$('.deletelanguage').click(function(){
    alert("success");
});

but if I set some <a> by ajax, $('.deletelanguage').click doesn't work.

for example

function CreateRow(jdata) { 
    $('#LangTable').append('<a class="deletelanguage">delete</a>');
}

$.ajax({        
    url: "/jobseeker/profile/",
    success: CreateRow
});

Now the $('.deletelanguage').click for the last <a> is not working.

jsfiddle example :http://jsfiddle.net/suhailvs/wjqjq/

Note: the CSS works fine here.

I want to make these newly appended <a> working with jQuery click.

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
suhailvs
  • 14,937
  • 8
  • 78
  • 88

7 Answers7

195

The problem is that .click only works for elements already on the page. You have to use something like on if you are wiring up future elements

$("#LangTable").on("click",".deletelanguage", function(){
  alert("success");
});
TGH
  • 37,121
  • 10
  • 94
  • 126
  • 16
    I was having this problem too, you have to be sure that you use a parent id or class in the first $('#here') and then the actual class of the button after "click", . Thanks for our answer, – rafaelmorais Oct 07 '14 at 11:05
  • 5
    like what @rafaelmsantos said, you have to go to up a parent id/class that doesn't get added dynamically – user1019042 Dec 18 '15 at 16:51
  • 1
    It also worth mentioning that the parent element should not be part of the AJAX call. In other words, the parent should not be generated by the AJAX call. – Kalimah Sep 29 '16 at 09:09
83

When you use $('.deletelanguage').click() to register an event handler it adds the handler to only those elements which exists in the dom when the code was executed

you need to use delegation based event handlers here

$(document).on('click', '.deletelanguage', function(){
    alert("success");
});
Arun P Johny
  • 365,836
  • 60
  • 503
  • 504
28
$('body').delegate('.deletelanguage','click',function(){
    alert("success");
});

or

$('body').on('click','.deletelanguage',function(){
    alert("success");
});
coolguy
  • 7,608
  • 9
  • 41
  • 70
8

Since the class is added dynamically, you need to use event delegation to register the event handler like:

$('#LangTable').on('click', '.deletelanguage', function(event) {
    event.preventDefault();
    alert("success");
});

This will attach your event to any anchors within the #LangTable element, reducing the scope of having to check the whole document element tree and increasing efficiency.

FIDDLE DEMO

palaѕн
  • 64,836
  • 15
  • 100
  • 121
5

Here's the FIDDLE

Same code as yours but it will work on dynamically created elements.

$(document).on('click', '.deletelanguage', function () {
    alert("success");
    $('#LangTable').append(' <br>------------<br> <a class="deletelanguage">Now my class is deletelanguage. click me to test it is not working.</a>');
});
Vond Ritz
  • 2,002
  • 12
  • 15
2

The click event doesn't exist at that point where the event is defined. You can use live or delegate the event.

$('.deletelanguage').live('click',function(){
    alert("success");
    $('#LangTable').append(' <br>------------<br> <a class="deletelanguage">Now my class is deletelanguage. click me to test it is not working.</a>');
});
prospector
  • 2,862
  • 1
  • 20
  • 38
  • This is deprecated and should not be used. As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). – msaad Jul 07 '14 at 02:00
  • Sure, but sometimes when stuck with older versions of jQuery live() is the only one that works, even against delegate(). – Wtower Sep 03 '14 at 12:55
1

I tested a simple solution that works for me! My javascript was in a js separate file. What I did is that I placed the javascript for the new element into the html that was loaded with ajax, and it works fine for me! This is for those having big files of javascript!!

pollux1er
  • 3,947
  • 2
  • 30
  • 35
  • I had this same issue and, curiously, inserting the .js at the end of the ajax loaded html, works! – JohnA10 Jan 15 '16 at 01:44
  • That's not good, then you will end up with duplicated js for each time you reload the component as the js gets moved out of the ajax loaded content! Beware of this solution. – nights Oct 19 '17 at 03:38