How do i add onclient click to my jquery button

924 Views Asked by At

I have dynamically created a button in jquery as shown below, how do i add the onclientclick attribute to it as i've done the class so when this button is added to the page, it does not cause a postback.

        var btnDelete = $('<button>', {
            class: "btnDeleteTaskListChild",
            text: "Delete",
            onclientclick: "return false;"
        });
2

There are 2 best solutions below

0
On BEST ANSWER

Here is a possible solution:

var btnDelete = $('<button>', {
            class: "btnDeleteTaskListChild",
            text: "Delete",
            onclientclick: "return false;"
        }).on('click', function(){
  alert('Delete Clicked');
  });

$('div').append(btnDelete); //for demo purpose, adding the delete button to div
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

0
On

There is nothing called as onclientclick in jQuery. You should use the click event handler and return false to disable post back. Try this.

 var btnDelete = $('<button>', {
            class: "btnDeleteTaskListChild",
            text: "Delete",
            click: function () { return false; }
        });