0

Currently I am having an issue exactly like in the article jQuery DataTables - Why click event handler does not work.

My code is something like below:

$('input[id^="remove_button"]').each(function(){
        $(this).click(function(){
            alert('hello');
        });         
    });


<table id="example<?php print $counter ?>" class="display" cellspacing="0" width="100%">
                <thead>
                    <tr>    
                        <th>cn</th>             

                    </tr>
                </thead>    

                <tfoot>
                    <tr>                        
                        <th>cn</th>             

                    </tr>
                </tfoot>            

<tbody> 
<?php                       
for ($i = 0; $i < $users["count"]; $i++) {

print '<td><input type=submit id=remove_button'.$i.' value="remove" /></td>';

  }

?>
</tbody>
</table>

I need to achieve as below:

$('table[id^="example"]').on('click', '.btn-details', function(){
   showModalDialog(this);
}

In my case tables are generating dynamically and so will have dynamic table ids. 'Remove Button' exists in each each row of every table.

I tried in many ways, but I am unable to make code to look like as above. Could anyone please help me on the issue above?

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
John
  • 281
  • 1
  • 4
  • 9
  • @JayBlanchard yeah, the above link is similar to my issue. But the problem from my end is: I am having dynamic tables, so not understanding how to write code correctly to make work above functionality. Could you please help me on this. – John Sep 21 '15 at 15:47
  • Remember that your event handler wire up acts on elements in the DOM at that time. If you're swapping out the table element then you need to use event delegation. I'd suggest delegating to whatever the parent of table is. If you post up a simple plunk with your example, I'll fork it and show you what I mean. – CainBot Sep 21 '15 at 15:51
  • I also do not see anything with the class `btn-details` Can you share a portion of the actual markup which is output by PHP? – Jay Blanchard Sep 21 '15 at 15:53
  • @JayBlanchard : In my case i do not have 'btn-details' , you can find this in this example, here: https://www.gyrocode.com/articles/jquery-datatables-why-click-event-handler-does-not-work/ My actual code, you can find above.. My tables are dynamic, having each row with some 'remove' button – John Sep 22 '15 at 17:20

0 Answers0