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

456
Views
How to delete table rows uisng javascript

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()
                       } 
            }

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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();
});
about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report

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>

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!