jquery click function not working

Hi,

I've got the following click function:
      $('.add_button').click(function() {
            alert(1);
      });

By pressing an other button, a IMG is dynamically added:
      $('.edit').click(function() {
            td2 = $(this).parents('td').next();
            td2.prepend('<img class="add_button" src="images/add.png">');
        });

Why is a new click on the dynamically added image not working?
DennieAsked:
Who is Participating?
 
Lukasz ChmielewskiConnect With a Mentor Commented:
Because for the dynamic content, you use live() selector, if that's what you mean.
http://api.jquery.com/live/
0
 
Gurvinder Pal SinghCommented:
a lot would depend on your html structure, please share the same.

also i quess, you are prepending the img at td level, which rather should be inside that td

please try to make it

$('.edit').click(function() {
            td2 = $(this).parents('td').next();
            var innerHTML = td2.html();
            td2.html('<img class="add_button" src="images/add.png"/>' + innerHTML );
        });
0
 
DennieAuthor Commented:
With prepend the img is added within the td, so that's not the problem. Can't share the entire html structure, its too much. But I'm pretty sure that's not the problem...

Your solution with html() doesn't work. any other ideas?
0
 
Lukasz ChmielewskiCommented:
Use

$('.add_button').live('click',function(){
            alert(1);
      });

Open in new window

0
 
DennieAuthor Commented:
Nice!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.