Alex
Alex

Reputation: 59

jQuery on click not working on ajaxified elements

Here's JS code:

    $('body').on('click', '.thumb', function() {
        alert(123);
    });

When you click on div with class .thumb it should alert the message, however, when i click - nothing happening.

Upvotes: 1

Views: 78

Answers (3)

Alexander Kim
Alexander Kim

Reputation: 18410

Use .mousedown() instead of .on('click'):

$('.thumb').mousedown(function() {
    alert(123);
});

With .mousedown() your JS code will fire only after Ajax request.

Upvotes: 1

mgsipl
mgsipl

Reputation: 199

This will help you out :

$('body').delegate('.thumb', 'click', function(e){
   alert('123');
});

Upvotes: 0

Kodr.F
Kodr.F

Reputation: 14400

Try this http://jsfiddle.net/d83p7/

$('.thumb').click(function(e){

 e.preventDefault(); 
alert("Horaa");

});

Or

$('body .thumb').click(function(e){

 e.preventDefault(); 
alert("Horaa");

});

OR

$('.thumb').live('click',function(e){

 e.preventDefault(); 
alert("Horaa");

});

OR

$('.thumb').bind('click',function(e){

 e.preventDefault(); 
alert("Horaa");

});

or

$('body').on('click', '.thumb', function(e) {

 e.preventDefault(); 
alert("Horaa");

});

place the examples inside

$( document ).ready(function() {
   //code here
});

Upvotes: 0

Related Questions