On click with an element that has been added to the page?


This question already has an answer here:


As you want to use in jQuery 1.6 so you can use .delegate() event handling.

$('.galleryImage').delegate('.delete', 'click', function() {

but would be better if you use jquery 1.7

and .on() event handler

$('.galleryImage').on('click', '.delete',function() {
According to comment

if .galleryImage is dynamically added then you should use a Static-element that is container of .galleryImage, that means the container should not dynamic.

Use delegate():

$('.galleryImage').delegate('.delete', 'click', function() {
    //do delete

Or use on() with a delegated selector if you are using jQuery 1.7+:

$('.galleryImage').on('click', '.delete', function() {
    //do delete

If the .galleryImage element is also dynamically added, you should use the selector which relates to the nearest non-dynamic element.

Do not use live()! It is slow, outdated and deprecated.

instead of on use delegate or live methods

http://api.jquery.com/live/ http://api.jquery.com/delegate/

Need Your Help

pageControl in navigationBar not centered

ios objective-c iphone swift uinavigationbar

i've added a pageControl to the titleView using the interface builder. The problem is that it is not centered at all. I could actually just do a lot of white spaces after the leftBarButton text, bu...

Object reference not set to an object (IP adress comparison)

c# mysql

Hey evryone I am trying to create an IP adress comparison so when the user logs in he/she needs to be tested to see if the IP on mysql database is ip on system's computer