Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how can we give the index of an array as an ID to an element

//Here the delete button gets a unique ID everytime, the ID is the array index of the element. 
array.forEach(function (element, index) {
      display =
        display +
        `<tr>
            <th scope="row">${index + 1}</th>
            <td>${element.first}</td>
            <td>${element.second}</td>
            <td>${element.third}</td>
            <td><button type="button" id="${index}" onclick="deleteNode(this.id)" class="btn btn-danger">Delete</button></td>
        </tr>`;
    });
 
PREVIOUS NEXT
Tagged: #give #index #array #ID #element
ADD COMMENT
Topic
Name
9+9 =