Ajit Bhapse
Ajit Bhapse

Reputation: 1

javascript execution on button click event

I am designing an application which is using a predefined library from DataTables to create the data table. I want to perform remove operation on Datatable, for which java script should get executed on button click event.

$(document).ready(function() {
    var table = $('#example').DataTable();
    $('#example tbody').on( 'click', 'tr', function () {
        if ($(this).hasClass('selected')) {
            $(this).removeClass('selected');
        }
        else {
            table.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    });

    $('#button').click(function() {
        table.row('.selected').remove().draw(false);
    });
});

HTML button is:

 <button type="button" class="btn btn-primary btn-sm">Remove</button>

Clicking this button should execute above script and the selected row should get deleted from the datatable.

Upvotes: 0

Views: 105

Answers (2)

Ashutosh Raj
Ashutosh Raj

Reputation: 1055

you are not actually selecting the right button . ways to select

1>To select the button either add "id='button'" without quotes in the html or 2>user your class to select the button if it is unique.

though both will work, first one is recommended.

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You need set the id(identifier) of button since you are using ID Selector ("#id")

<button id="button" type="button" class="btn btn-primary btn-sm">Remove</button>

Upvotes: 1

Related Questions