Solved

jquery click function not working

Posted on 2011-02-21
5
269 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:Dennie
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34941901
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
 

Author Comment

by:Dennie
ID: 34941960
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
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34941993
Because for the dynamic content, you use live() selector, if that's what you mean.
http://api.jquery.com/live/
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34942002
Use

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

Open in new window

0
 

Author Closing Comment

by:Dennie
ID: 34942019
Nice!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question