0

I want to delete every row from the table using javascirpt Here's the code I tried using .remove function but it did'nt work out...

HTML TABLE CODE

<div class="card-body">
                                            <table class="table text-center">
                                                <thead>
                                                    <tr>
                                                        <th scope="col">#</th>
                                                        <th scope="col">Name</th>
                                                        <th scope="col">Total</th>
                                                        <th scope="col">Reaming Paid</th>
                                                        <th scope="col">To Be Paid</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="table_body">
                                                   <tr>
                                                 <td>2</td>
                                                 <td> mess fee </td>
                                                 <td> 2500 </td>
                                                 <td>0 </td>
                                                 <td>  <input type="number" id="remaing_amount" name="remaing_amount[]" class="form-control" placeholder="Enter Paid Amount"></td>
                                                 </tr>

                                                </tbody>
                                            </table>
                                        </div>

JAVASCRIPT CODE


 if(tablebody.children.length > 0)
            {

                  for (let i = 0; i < tablebody.children.length; i++)
                        {
                          tablebody.children[i].remove()
                       } 
            }

2
  • children is a live list. While you remove elements, the subsequent element indexes decrease, meaning you miss elements. Don't use index-based looping for removing elements. Commented Apr 27, 2022 at 10:15
  • Please take at least basic care of code formatting in your questions on StackOverflow. Commented Apr 27, 2022 at 10:17

5 Answers 5

1

Explination

This will get all tr (rows) for the tables body. Then it will delete (remove) any that it finds

Solution

let trs = document.querySelectorAll('#table_body tr');

trs.forEach((tr)=>{
        tr.remove();
});
Sign up to request clarification or add additional context in comments.

Comments

1

Find all table rows, iterate over them using for of then remove each row with Element.remove().

const rows = document.querySelectorAll("#table_body tr");

for (const row of rows) {
  row.remove();
}
<div class="card-body">
  <table class="table text-center">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">Total</th>
        <th scope="col">Reaming Paid</th>
        <th scope="col">To Be Paid</th>
      </tr>
    </thead>
    <tbody id="table_body">
      <tr>
        <td>2</td>
        <td>mess fee</td>
        <td>2500</td>
        <td>0</td>
        <td>
          <input
            type="number"
            id="remaing_amount"
            name="remaing_amount[]"
            class="form-control"
            placeholder="Enter Paid Amount"
          />
        </td>
      </tr>
    </tbody>
  </table>
</div>

Comments

0

If you want to delete all tr maybe you should do something like this

document.getElementById('table_body').innerHTML = ''
<div class="card-body">
  <table class="table text-center">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">Total</th>
        <th scope="col">Reaming Paid</th>
        <th scope="col">To Be Paid</th>
      </tr>
    </thead>
    <tbody id="table_body">
      <tr>
        <td>2</td>
        <td> mess fee </td>
        <td> 2500 </td>
        <td>0 </td>
        <td> <input type="number" id="remaing_amount" name="remaing_amount[]" class="form-control" placeholder="Enter Paid Amount"></td>
      </tr>

    </tbody>
  </table>
</div>

2 Comments

OP wanted to remove all rows, not everything inside the table body
@Undo what else there is in a tbody tag?
0

Try this way...

const tBody = document.querySelector('#table_body');
const tRow =document.querySelector('#table_body tr')
if (tBody.contains(tRow) {
 tRow.remove();
}

Comments

0

try this code:

<div class="card-body">
   <table class="table text-center">
      <thead>
         <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Total</th>
            <th scope="col">Reaming Paid</th>
            <th scope="col">To Be Paid</th>
         </tr>
      </thead>
      <tbody id="table_body">
         <tr id = "row1">
            <td>2</td>
            <td> mess fee </td>
            <td> 2500 </td>
            <td>0 </td>
            <td>  <input type="number" id="remaing_amount" name="remaing_amount[]" class="form-control" placeholder="Enter Paid Amount"></td>
         </tr>
      </tbody>
   </table>
   <button onclick = "deletetr()"> 
   Click here
   </button> 
   <script> 
      function deletetr() {
          document.getElementById("row1").remove();
      }
   </script> 
</div>          
            

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.