0

Very new to JQuery, I have a data table integrated in my application. I have added two buttons in the last column, but facing issues with binding it with particular event.

Here is my code to generate last column of the table.

   "mRender": function (data, type, full) {
    console.log('>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');
    console.log(full);
    if(full.status) {
        return '<button href="#"' + 'id="'+ data + '">Edit</button> <input type="button" href="#"' + 'id="disable-'+ data + '" value="Disable"></input>';                       
    }
    else {
        return '<button href="#"' + 'id="'+ data + '">Edit</button> <input type="button" href="#"' + 'id="enable-'+ data + '" value="Enable"></input>';
    }
}

based on status I am showing button either enable/disable.

Now I want to bind onclick event using JQuery.

I have bind one button using following way.

$('#tenantTable tbody').on( 'click', 'button', function () {        
    var data = table.row( $(this).parents('tr') ).data();
});
$('#tenantTable tbody').on( 'click', 'input[id="enable*"]', function () {
    var data = table.row( $(this).parents('tr') ).data();
    console.log('>> DISABLE <<');
});
$('#tenantTable tbody').on( 'click', 'input[id="disable*"]', function () {
    var data = table.row( $(this).parents('tr') ).data();
    console.log('>> DISABLE <<');
});

It is not working, Is there a way I can do the above?

4
  • Is the "way above" not working? Commented May 3, 2017 at 9:19
  • No it is not, no event is being fired when I click on the "Enable"/"disable" button. Commented May 3, 2017 at 9:19
  • I believe you only have to change: 'input[id="enable*"]' to 'input[id*="enable"]' and 'input[id*="disable"]' Commented May 3, 2017 at 9:21
  • 1
    working :) :) thanks :) :) you can post your answer I will accept it Commented May 3, 2017 at 9:27

1 Answer 1

1

The on() method in this way is a good choice to delegate event handlers. The selector parameter you use however should be changed slightly:

$('#tenantTable tbody').on( 'click', 'input[id*="enable"]', function () {
    // logic
});

$('#tenantTable tbody').on( 'click', 'input[id*="disable"]', function () {
    // logic
});

The *= selector translates in a contains selector. An alternative for your solution could be a startswith selector ^=

This logic makes it possible to have dynamic id's for example:

<input type="button" id="enable-5" data-action="enable" data-id="5" value="my button" />

The selector could be something like:

$(element).on('click', '[id^="enable-"]', function(){/*this: element where the id starts with enable-*/});
$(element).on('click', '[data-action="enable"]', function(){/*this: element where the data-action equals to enable*/});
$(element).on('click', '[data-id]', function(){/*this: element where data-id is defined*/});
Sign up to request clarification or add additional context in comments.

2 Comments

can you please add some example for startsWith selector ?
I provided some examples as requested. Also a little hint to the idea of using data-attributes if more dynamics are required in the future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.