views:

69

answers:

3

Hi,

I have the following code in my application:

$(".deleteproduct").click(function() {
    id = this.id;
    alert("id: " + id);
});

If i run my site nothing happens when I click on an element with the class deleteproduct. But when I place breakpoints before line 1,2 and 3 it works like a charm.

The elements with class deleteproduct are loaded through AJAX call right behind $(document).ready(function(). The .click code is underneath it.

This seems really weird to me. It's working if breakpoints in Firebug are used, but not when there are not...

A: 

Sounds like a race condition to me. Have you remembered to put that code inside

$().ready(function(){
    ....
});

That is so that the document has completely loaded before you try to script against the DOM.

Sean Kinsey
He could shorten it to $(function() {...});
Bob
+3  A: 

If you are loading your .deleteproduct element via Ajax, you might need to define the click function using jQuery's live event handler.

$(".deleteproduct").live('click', function(){
    id = this.id;
    alert("id: "+id);
});

or put the function above in the Ajax callback.

fudgey
That did it. I tried the .bind. But apparently that is something different. Thanks for you're help. This will be marked solved as soon as I may. Howcome everything works with firebug activated?
Dante
Why it works with firebug? I don't know LOL.
fudgey
A: 
$(document).ready(function(){
    $(".deleteproduct").click(function(){
        var id = $(this).attr('id');
        alert("id: "+id);
    });
});