108

I have a piece of jQuery that loops through each element in a given div( #container) and does a javascript alert each time a span is clicked. This works fine if the <span>'s are static.

However, if I use a piece of code like:

$(someLink).click(function(){
   $("#container").html( <new html with new spans> )
});

The jQuery code doesn't fire off. Oddly enough though

My question is : Is there a reason my Click events don't work for dynamically created items? I assume I will have to add something into my document ready or heartbeat-script (which is fired every 100 miliseconds) to hook up the events??

JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68

8 Answers8

163

Do this:

 $( '#wrapper' ).on( 'click', 'a', function () { ... })

 $( 'body'     ).on( 'click', '.your_dynamic_elem_css_selector', function () { ... })  # use body as wrapper static elem

where #wrapper is a static element in which you add the dynamic links.

So, you have a wrapper which is hard-coded into the HTML source code:

<div id="wrapper"></div>

and you fill it with dynamic content. The idea is to delegate the events to that wrapper, instead of binding handlers directly on the dynamic elements.


Btw, I recommend Backbone.js - it gives structure to this process:

var YourThing = Backbone.View.extend({

    // the static wrapper (the root for event delegation)
    el: $( '#wrapper' ),

    // event bindings are defined here 
    events: {
        'click a': 'anchorClicked'
    },

    // your DOM event handlers
    anchorClicked: function () {
        // handle click event 
    }

});

new YourThing; // initializing your thing
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
91

source: this post

if you created your elements dynamically(using javascript), then this code doesn't work. Because, .click() will attach events to elements that already exists. As you are dynamically creating your elements using javascript, it doesn't work.

For this you have to use some other functions which works on dynamically created elements. This can be done in different ways..

Earlier we have .live() function

$('selector').live('click', function()
{
//your code
});

but .live() is deprecated.This can be replaced by other functions.

Delegate():

Using delegate() function you can click on dynamically generated HTML elements.

Example:

$(document).delegate('selector', 'click', function()
{
 //your code
});

EDIT: The delegate() method was deprecated in version 3.0. Use the on() method instead.

ON():

Using on() function you can click on dynamically generated HTML elements.

Example:

$(document).on('click', 'selector', function()
{
// your code
});
Leone
  • 13
  • 3
Bharat Bhushan
  • 1,315
  • 3
  • 18
  • 24
8

Try something like

$("#container").on('click', 'someLinkSelector', function(){ $("#container").html( <new html with new spans> ) });

You basically need to attach your events from a non-dynamic part of the DOM so it can watch for dynamically-created elements.

Chris
  • 971
  • 7
  • 15
6

You have to add click event to an exist element. You can not add event to dom elements dynamic created. I you want to add event to them, you should bind event to an existed element using ".on".

$('p').on('click','selector_you_dynamic_created',function(){...});

.delegate should work,too.

rocLv
  • 548
  • 6
  • 15
  • This is a great answer to explain what the real issue is. The other answers give the solution, but this explains why it works. Good job @rocLv ! My issue is that I'm adding elements to a dom element, but they aren't getting bound with the event for some reason. I wonder if I can set the on click event as I create the elements. – Urasquirrel Sep 10 '20 at 23:14
  • What exactly is `selector_you_dynamic_created` is it a class, an id? – warmwhisky Mar 30 '21 at 17:19
4
$("#container").delegate("span", "click", function (){
    alert(11);
});
elrado
  • 4,960
  • 1
  • 17
  • 15
  • It used to be the right way to do it, do not know if it is any different now (did not use jQuery for some time now). – elrado Feb 07 '18 at 08:12
2

Using .click will only attach events to elements that already exist.

You need to use a function which monitors for dynamically created events - in older versions of JQuery this was .live(), but this has been superceded by .on()

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
BonyT
  • 10,750
  • 5
  • 31
  • 52
1

I faced this problem a few days ago - the solution for me was to use .bind() to bind the required function to the dynamically created link.

var catLink = $('<a href="#" id="' + i + '" class="lnkCat">' + category.category + '</a>');
catLink.bind("click", function(){
 $.categories.getSubCategories(this);
});

getSubCategories : function(obj) {
 //do something
}

I hope this helps.

MarkD
  • 33
  • 1
  • 6
0

Use the new jQuery on function in 1.7.1 -

http://api.jquery.com/on/

Chris Dixon
  • 9,147
  • 5
  • 36
  • 68