Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery click not working for dynamically created items [duplicate]

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??

like image 754
JustAnotherDeveloper Avatar asked Feb 28 '12 15:02

JustAnotherDeveloper


People also ask

Does jQuery work on dynamic content?

You have to add the selector parameter, otherwise the event is directly bound instead of delegated, which only works if the element already exists (so it doesn't work for dynamically loaded content). The jQuery set receives the event then delegates it to elements matching the selector given as argument.

Which method is used to bind a callback method for an event for DOM elements added at runtime?

You can use the live() method to bind elements (even newly created ones) to events and handlers, like the onclick event.


2 Answers

Do this:

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

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 
like image 140
Šime Vidas Avatar answered Sep 20 '22 18:09

Šime Vidas


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 }); 
like image 44
Bharat Bhushan Avatar answered Sep 17 '22 18:09

Bharat Bhushan