Solved

jquery click function not working

Posted on 2011-02-21
5
267 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now