Jump to content

Everytime I send a new data it just adds up to the list. Is there a vanilla JS code that can delete the old li first then add?

hinzwifi
function gotData(data) {

          var scores = data.val();

          var keys = Object.keys(scores);

          for (var i = 0; i < keys.length; i++) {
            var k = keys[i];

            var name = scores[k].name;
            var score = scores[k].score;
            console.log(name,score);
            
        parentElement = document.getElementById('scorelist');
        childElement = document.createElement('li');
        appendChildElement = parentElement.appendChild(childElement);
        appendChildElement.innerHTML = name + ': ' + score;
      
        }
        
        
        }

 

image.png

Link to comment
Share on other sites

Link to post
Share on other sites

What is your aim? Is it only supposed to display one or a limited number? If the latter, then do you want to remove the first one or the last one?

If the first one, then just replace the element. If you have a limited number, then one way would be to delete the parent element and recreate the number of elements you need.

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, hinzwifi said:

function gotData(data) {

          var scores = data.val();

          var keys = Object.keys(scores);

          for (var i = 0; i < keys.length; i++) {
            var k = keys[i];

            var name = scores[k].name;
            var score = scores[k].score;
            console.log(name,score);
            
        parentElement = document.getElementById('scorelist');
        childElement = document.createElement('li');
        appendChildElement = parentElement.appendChild(childElement);
        appendChildElement.innerHTML = name + ': ' + score;
      
        }
        
        
        }

 

image.png

 

var elem = document.querySelector('#some-element');
elem.parentNode.removeChild(elem);

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

My aim is to display all the scores that the participants sent to the firebase database both the highscore and their nickname

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, shadow_ray said:

 



var elem = document.querySelector('#some-element');
elem.parentNode.removeChild(elem);

 

 

var elem = document.querySelector('ol');
elem.parentNode.removeChild(elem);

So like I added that then nothing displayed

Edited by hinzwifi
im retard
Link to comment
Share on other sites

Link to post
Share on other sites

Btw this is my html code

<div id="main">
    <ol id="scorelist" >

  </ol>
  </div>

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to clear all the li's from the list you can just set the innerHTML of the scoreList element to empty

parentElement = document.getElementById('scoreList');
parentElement.innerHTML = ''; // clear all li's from list

 

Link to comment
Share on other sites

Link to post
Share on other sites

if you only want to delete the first li in the list:

var elem = document.querySelector('#scorelist > li:nth-child(1)');
elem.parentNode.removeChild(elem);

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×