For dynamically created elements, you must use event delegation :
The delegation event allows us to attach one event listener to the parent element, which will fire for all children matching the selector, whether these children exist now or are added in the future.
$('body').on('click', 'a.showMore', function(event) { var rowindex = $(this).closest('tr').index(); console.debug('rowindex', rowindex); });
Note that you do not need a space between a and .showmore , because a .showmore will select any elements with the showmore class, which is a child of the binding.
In addition, .parent() is a method, so you need () after parent if you want to use .parent() .
Another suggestion is that instead of the multiple parent() method, you can use .closest ()
Felix
source share