Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
How to delete HTML table row with stored <td> index variable

I have a web page that lists multiple rows to a table. Each row has a button that is used to open a confirmation modal if the user wants to remove the row.

Is there any way to pass the row information (index or something) to the submit button element that is outside the table-element and which executes the actual removing function?

I know that <input type="button" value="Delete" onclick="deleteRow(this)"/> would do the trick if its inside the tr-element, but this is not the case.

My HTML code:

            <table id="myTable">
                <tr>
                    <th><fmt:message key="time"/></th>
                    <th><fmt:message key="name"/></th>
                </tr>
                <c:forEach items="${myList}" var="item">
                    <tr>
                        <td>${item.timeStamp}</td>
                        <td>${item.name}</td>
                        <td><input type="button" onclick="openModal"/>Delete</td> //here I want to store this rows index
                    <tr>
               </c:forEach>
            </table>
            <div class="modal-container" id="modal_container">
                <div class="modal">
                    <p>Do you want to remove item?</p>
                    <button type="button" id="close">Cancel</button>
                    <button type="submit" id="remove" onclick="deleteRow(this)">Yes</button> //here I want to use the stored index
               </div>
            </div>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I never used Jsp but you can try something like this (vanilla javascript) :

var form = window.querySelector('#myTable');
var buttonSubmit = window.querySelector('#remove'); 

// Events
buttonSubmit.addEventListener('click', submitMe);
form.addEventListener('submit', submitMe);

// Submit function
function submitMe(e) {
  e.preventDefault(); // 'Revoke' current action of the event (use to stop input submit request)
  const formData = new FormData(form);
  formData.append('CustomKey', 'CustomValue');
  // Send new XMLHttpRequest here
}

Some docs :

  • FormData MDN
  • XMLHttpRequest
  • preventDefault (event function)
about 4 years ago · Juan Pablo Isaza Report

0

You need global valiable to save the row:

var row;

then the function for open the modal and save the row:

// When the user clicks on the button, open the modal and save the row
let openModal = function(scope) {
  modal.style.display = "block";
  row = scope;
}

and then the function to delete the row if user press yes:

// When the user clicks on the yes button, delete the row, close the modal and reset row
function deleteRow() {
  var parent = row.parentNode.parentNode;
  parent.parentNode.removeChild(parent);
  row = "";
  modal.style.display = "none";
}

Your button should open the modal dialog

<td><input type="button" onclick="openModal(this)"/></td>

and if user press yes you call the delete function

<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Do you want to remove item?</p>
    <button type="submit" id="remove" onclick="deleteRow()">Yes</button>
  </div>
</div> 

on my codepen page i solved this for you.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!