0

I am trying to delete the first row of a table using javascript.

My table is:

 <table id="rankings-table" class="table">
    <thead>
        <tr>
        <th>Ranking</th>
        <th>Full name</th>
        <th>points</th>
        </tr>
    </thead>

    <tbody>
        <tr>
        <td>1</td>
        <td>dom</td>
        <td>1,340</td>
        </tr>
        <tr>
        <td>2</td>
        <td>Naoimi</td>
        <td>932</td>
        </tr>
        <tr>
        <td>3</td>
        <td>Sarah</td>
        <td>1,120</td>
        </tr>
    </tbody>
    </table>

and in javascript I am trying:

<script type="text/javascript">

    const rankingsBody = document.querySelector('#rankings-table > tbody ');

     function deleteRankings() {
        console.log(rankingsBody);

        rankingsBody.firstChild.remove();
        //rankingsBody.removeChild(rankingsBody.firstChild);
        }


    document.addEventListener("DOMContentLoaded", () => { deleteRankings(); });

</script>

neither the remove() or removeChild() is working.

why? How do I make it work with rankingsBody variable?

4
  • rankingsBody.removeChild(rankingsBody.firstChild) - You have to pass the node to remove to .removeChild() Commented Jan 29, 2020 at 22:47
  • 1
    console.log(rankingsBody.firstChild) <-- first child plays some mean tricks Commented Jan 29, 2020 at 22:51
  • @epascarello lol! Commented Jan 29, 2020 at 22:56
  • you should take a look at my (late) answer i think it will serve you a lot more Commented Jan 29, 2020 at 23:03

5 Answers 5

1

The line break after your <tbody> tag is a text node. firstChild gets the first node, not the first element. In your current code, replace firstChild with firstElementChild and it should work fine.

function deleteRankings() {
    console.log(rankingsBody);

    rankingsBody.firstElementChild.remove();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Simply use deleteRow => https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/deleteRow

const rankingsBody = document.querySelector('#rankings-table tbody')
  ;
bt_del.onclick=_=>
  {
  if (rankingsBody.rows.length) 
    { rankingsBody.deleteRow(0)  }
  else
    { alert('no more row to delete') }
  }
table { border-collapse: collapse; }
th, td { border: 1px solid gray;  padding: .3em 1em; } 
<br> 
<button id="bt_del">delete first row </button>
  
<br> <br>  
  
<table id="rankings-table">
  <thead>
    <tr> <th>Ranking</th> <th>Full name</th> <th>points</th> </tr>
  </thead>
  <tbody>
    <tr> <td>1</td> <td>dom</td>    <td>1,340</td> </tr>
    <tr> <td>2</td> <td>Naoimi</td> <td>  932</td> </tr>
    <tr> <td>3</td> <td>Sarah</td>  <td>1,120</td> </tr>
  </tbody>
</table>

3 Comments

:what is the underscore in this bt_del.onclick=_=> mean? whats the difference between using bt_del.onclick=_=> and bt_del.onclick=() =>
Thanks a lot :-)
0

You could use Element.children, which will only return actual Nodes.

let rankingsBody = document.querySelector('#rankings-table > tbody');

document.addEventListener("DOMContentLoaded", deleteRankings);

function deleteRankings() {
    rankingsBody.removeChild(rankingsBody.children[0]);
}
<table id="rankings-table" class="table">
    <thead>
        <tr>
        <th>Ranking</th>
        <th>Full name</th>
        <th>points</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        <td>1</td>
        <td>dom</td>
        <td>1,340</td>
        </tr>
        <tr>
        <td>2</td>
        <td>Naoimi</td>
        <td>932</td>
        </tr>
        <tr>
        <td>3</td>
        <td>Sarah</td>
        <td>1,120</td>
        </tr>
    </tbody>
</table>

Comments

0

You can try rankingsBody.firstElementChild.remove() as firstElementChild will give you the tr element.

Comments

0

This is another way of deleting the first row of your table. I hope it helps.

function deleteFirstRowOfTable(tableId)
{   //get the text in the first <tbody> tag of the table
    var tableBody = document.getElementById(tableId).tBodies[0].innerHTML;
    var index = tableBody.indexOf("</tr>");//find the first occurence of the end tag
    if(index !== -1)//if </tr> end tag is found
    {
        tableBody = tableBody.slice(index).replace('</tr>','');//remove the first row from the table body
        //set the new table body
        document.getElementById(tableId).tBodies[0].innerHTML = tableBody;
    }
} 

Usage example:

deleteFirstRowOfTable('rankings-table'); 
//the output is as shown below

enter image description here

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.