简体   繁体   中英

How to pass paramters in dynamically created functions in Javascript?

I have created one form and form after submitting the value I want to show them in a table. In table I have one section where I want to delete or edit a particular user on a button. But when I am passing the id to that function I am getting error saying that refernece error occurred!

function getEmployeeDetails() {
 for (var i = 0; i < userArray.length; i++) {
    var tr = "<tr>";

    tr +=
      "<td class='table-data'/td>" +
      userArray[i].id +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].name +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].email +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].gender +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].role +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].english +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].hindi +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].othersLang +
      "</td>" +
      "<td class='table-data'/td>" +
      userArray[i].description +
      "</td>" +
      "<td class='table-data'/td>" +
      "<button onclick='deleteUser( userArray[i].id )'>Delete</button> || <button onclick='editUser( userArray[i].id )'>Edit</button>" +
      "</td>" +
      "</tr>";

    tbody.innerHTML += tr;
  }
}

function deleteUser(id) {
  console.log("delteuser", typeof id, id);
}
function editUser(id) {
  console.log("edit", id);
}

Where I have made the mistakes?

Change

  <button onclick='deleteUser( userArray[i].id )'>

To something like

 <button onclick='deleteUser('+userArray[i].id+')'>

In your current attempt you are not inserting the value of userArray[i].id but the variable userArray[i].id which is kind of nonsense.

Same story with the editUser function

The problem is in the string concatenation you are using in the onClick event.

You can use a backtick character instead. Copy-paste and check the below code.

<html>
  <body>
    <table id="table"></table>
    <script>
      getEmployeeDetails();
      function getEmployeeDetails() {
        let userArray = [{ id: 1 }, { id: 2 }];
        var tr = "";
        for (var i = 0; i < userArray.length; i++) {
          tr +=
            `<td class="table-data">
              <button onclick="deleteUser(` +
                      userArray[i].id +
                      `)">Delete</button> || <button onclick="editUser( ` +
                      userArray[i].id +
                    `)">Edit</button>
             </td>`;
        }
        document.getElementById("table").innerHTML = tr;
      }
      function deleteUser(id) {
        console.log("delteuser", typeof id, id);
      }
      function editUser(id) {
        console.log("edit", id);
      }
    </script>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM