nimra asad
nimra asad

Reputation: 179

How to append a button when already appending a row in javascript

I am appending a row in HTML table using java script. I also need to append a button as one of the Table data of the row. That button should have on click event as well. how to do so? I cant get results out from any previous answers Please help. Below is my code.

$(document).ready(function () {
        $.ajax({
            url: '/api/UserApi/',
            type: 'GET',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
              
                alert('User Added Successfudly');

                for (var i = 0; i < data.length; i++) {
                         $("#absa").append("<tr><td>" + data[i].user_Name + "</td><td>" + data[i].Address + "</td><td>" + data[i].Email + "</td><td>" + data[i].MobileNo + "</td><td><button onclick="update()"/></td></tr>");
                }
            },
            error: function () { alert('User not Added'); }
        });

    });

Upvotes: 0

Views: 38

Answers (1)

Ankit vadariya
Ankit vadariya

Reputation: 1263

You need to pass event function as a string. Dom will handle it

$(document).ready(function () {
        $.ajax({
            url: '/api/UserApi/',
            type: 'GET',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {

                alert('User Added Successfudly');

                for (var i = 0; i < data.length; i++) {
                         $("#absa").append("<tr><td>" + data[i].user_Name + "</td><td>" + data[i].Address + "</td><td>" + data[i].Email + "</td><td>" + data[i].MobileNo + "</td><td><button onclick='update()'/></td></tr>");
                }
            },
            error: function () { alert('User not Added'); }
        });

    });

Updated

<button onclick='update()'/>

Upvotes: 1

Related Questions