0

I was trying on button click delete the row from table in javascript.I am able to do so usig jquery, but is it possible to do using pure vanilla javascript.

Is there way to use click event and delete the row ?

$(".delete").click(function() {
  $(this).closest("tr").remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Row 1</td>
    <td><button class="delete">Del</button></td>
  </tr>
  <tr>
    <td>Row 2</td>
    <td><button class="delete">Del</button></td>
  </tr>
  <tr>
    <td>Row 3</td>
    <td><button class="delete">Del</button></td>
  </tr>
</table>
PURU
  • 107
  • 2
  • 9
  • Does this answer your question? [Vanilla JavaScript .closest without jQuery](https://stackoverflow.com/questions/28406403/vanilla-javascript-closest-without-jquery) – Shoejep Nov 17 '20 at 07:45

3 Answers3

0

I added onClick for each button

function deleteRow(button) {
     const tr = button.parentNode.parentNode;
     tr.parentNode.removeChild(tr);
}
<table id="table">
  <tr>
    <td>Row 1</td>
    <td><button onClick="deleteRow(this);" class="delete">Del</button></td>
  </tr>
  <tr>
    <td>Row 2</td>
    <td><button onClick="deleteRow(this);" class="delete">Del</button></td>
  </tr>
  <tr>
    <td>Row 3</td>
    <td><button onClick="deleteRow(this);" class="delete">Del</button></td>
  </tr>
</table>
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
0

You should create a function for onclick event for the every <td>Del</td>. When you click on it you should get the tr parent element something like this:

And don't forget that function to the onclick event for the buttons!

const onDelete = (event) => {
  const buttonElement = event;
  const tdElement = buttonElement.parentNode;
  const trElement = tdElement.parentNode;

  trElement.remove();
}
<table>
  <tr>
    <td>Row 1</td>
    <td><button class="delete" onclick="onDelete(this)">Del</button></td>
  </tr>
  <tr>
    <td>Row 2</td>
    <td><button class="delete" onclick="onDelete(this)">Del</button></td>
  </tr>
  <tr>
    <td>Row 3</td>
    <td><button class="delete" onclick="onDelete(this)">Del</button></td>
  </tr>
</table>
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
QuazBuzz
  • 1,112
  • 1
  • 8
  • 18
0

Everything in jQuery can be replicated in vanilla javascript. With the same html, I would do it like this:

const buttons = document.querySelectorAll('.delete');

const addRemoveListener = button => {
    button.addEventListener('click', removeListener);
}

const removeListener = event => {
  const button = event.currentTarget;
  const cell = button.parentNode;
  const row = cell.parentNode;
  row.remove();
}

buttons.forEach(addRemoveListener);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Row 1</td>
    <td><button class="delete">Del</button></td>
  </tr>
  <tr>
    <td>Row 2</td>
    <td><button class="delete">Del</button></td>
  </tr>
  <tr>
    <td>Row 3</td>
    <td><button class="delete">Del</button></td>
  </tr>
</table>
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
P.E. Joëssel
  • 1,393
  • 10
  • 18