Link to home
Start Free TrialLog in
Avatar of Dennie
Dennie

asked on

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?
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

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 );
        });
Avatar of Dennie
Dennie

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use

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

Open in new window

Avatar of Dennie

ASKER

Nice!