How to define event of two button in datatable ?

How to define event of two button in datatable ?

headshot9xheadshot9x Posts: 59Questions: 16Answers: 1
edited December 2015 in Free community support

Hello guys, In datatable , i have two buttons, I want to define event each of button

              {
                   "mData": null,
                   "aTargets": [-1],
                   "defaultContent": '<button type="button" id="bt1" class="btn1">Button1</button>'
               },
               {
                   "mData": null,
                   "aTargets": [-2],
                   "defaultContent": '<button type="button" id="bt2" class="btn2">Button2</button>'
               },

And event jquery as follow:

$('#table tbody>bt1').on('click', 'button', function (e) {
       console.log("button 1 is click");
   });
$('#table tbody>bt2').on('click', 'button', function (e) {
       console.log("button 2 is click");
   });

When i click button 1 or button 2, i'm not see log in devtool Chrome.
Can you tell me about this, some mistake or wrong in here. ? Thank you.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    Answer ✓

    I would suggest using:

    $('#table tbody').on('click', 'button.btn1', function (e) {
           console.log("button 1 is click");
       });
    

    And likewise for button 2. See the jQuery events documentation for how this works.

    At the moment the selector '#table tbody>bt1' is not selecting anything (there are no direct decedents of the tbody which have have the tag name bt1. You might want to take a look into CSS selectors a bit as well since that is how jQuery typically selects elements.

    Allan

This discussion has been closed.