Solved

jquery click function not working

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS Position not returning 2 46
How to drill down using class name and find the text? 25 32
Polygon I google maps 1 14
how would you interpret lines 3 29
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

914 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

23 Experts available now in Live!

Get 1:1 Help Now