-4

I have a button which adds rows to my table. but the property of Delete button dose work as it has been added after readyFucntion have been called

enter code here
Subayan
  • 111
  • 1
  • 1
  • 7

1 Answers1

2

Assuming the elements are all added to a common ancestor, use event-delegation with on() (in jQuery 1.7+):

$('#commonAncestorID').on('click', '.newElementsClass', function(){
    // do something when the elements with 'newElementsClass' are clicked
});

Or using delegate() (jQuery < 1.7):

$('#commonAncestorID').delegate('.newElementsClass', 'click', function(){
    // do something when the elements with 'newElementsClass' are clicked
});

You can, of course, bind delegated events to any element that's an ancestor of the newly-added elements, such as the body or to the document itself; but binding to the closest common parent prevents the events bubbling all the way through the DOM, which avoids needless performance penalties.

References:

David says reinstate Monica
  • 230,743
  • 47
  • 350
  • 385
  • Thankx a lot @david can u also tell do i need to write this code in readyMethod or i can write it out side also – Subayan Jun 17 '13 at 15:57
  • It should be done in either `$(document).ready(function(){ /* in here */});`, or be in a script tag before the `

    ` closing tag. Otherwise the elements your binding to won't be there, yet.

    – David says reinstate Monica Jun 17 '13 at 17:12