source: this post
if you created your elements dynamically (using javascript) then this code does not work. Because .click () attaches events to existing elements. Since you dynamically create your elements using javascript, this does not work.
To do this, you need to use some other functions that work with dynamically created elements. This can be done in many ways.
We used to have a live () function
$('selector').live('click', function() {
but .live () is deprecated. It can be replaced by other functions.
delegate ():
Using the delegate () function, you can click on dynamically generated HTML elements.
Example:
$(document).delegate('selector', 'click', function() {
ON ():
Using the on () function, you can click dynamically generated HTML elements.
Example:
$(document).on('click', 'selector', function() {
Bharat Bhushan Jul 01 '13 at 9:25 am 2013-07-01 09:25
source share