I am trying to figure out how to get localStorage to work here. I am trying to make a high score list that takes the user’s ending score on the win condition I have and append it to a p or div at the least, and then save it. I have this code:
function saveHighScore() {
let date = new Date()
let highScoreDivNew = document.createElement("div")
let highScoreNumbers = document.innerHTML = `Your high score is ${pikmin.numberOf} on ${date}!`
highScoreDiv.appendChild(highScoreDivNew)
document.body.insertBefore(highScoreNumbers, highScoreDivNew)
localStorage.setItem("highScoreContent", highScoreNumbers)
}
I can supply any other code if needed. This code doesn’t really "run" or do anything without the rest of my code and images, elements, selectors, etc. I already got localStorage working using this code:
dayTimer.innerHTML = `<p>You have taken ${dayCounter}/6 days!</p>`
dayTimer.class = "save-timer"
function saveDayTimer() {
localStorage.setItem("dayCounter", dayTimer)
}
This displays it as 0/6 in my code at the moment and ticks up from 0 -> 1 -> 2 etc when the user ends a round.
But since I’m not using two "selectors" as it were, not sure what to call those, I can’t figure it out. I’ve also been working on this project all week so my brain is a little mush. When I run this and "win" a round with a "score", it gives me this error.
script.js:63 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
at saveHighScore (script.js:63:18)
at updateDayTimer (script.js:138:9)
at HTMLButtonElement.<anonymous> (script.js:103:5)
Which I understand means it’s not an HTML DOM element being appended, but since I’m trying to append highScoreDivNew
I thought that would work. I’ve lurked around on SO for a while looking for an answer to this and maybe it’s just mush-brain, but I can’t seem to find any answers that fit this or that I can make work.
I’ve mostly tried making a new variable with document.createElement, but as far as I could tell the innerHTML is supposed to be inside the new div element? The MDN docs are a little confusing on .createElement.
My goal is to dynamically insert high score elements into my highScoreDiv when the player reaches a win state, which is a div that comes up when I press a button. I don’t necessarily need to store and retrieve it, just display it, so I’m not looking for answers using json or jquery or whatnot since I don’t know those languages yet.
Entirety of Code: Code link
2
Answers
Here’s how you can do it without using JSON:
The document.body.appendChild() method will append the specified element as a child of the element. I apologize for the confusion.
To append the high score into a specific div that’s inside the body, you need to target that div and then use the appendChild() method to append the new high score element as a child of that div.
Assuming you have a div with the id "highScoreDiv" in your HTML where you want to display the high scores, you can modify the saveHighScore() function like this:
Make sure that you have a div with the id "highScoreDiv" in your HTML where you want the high scores to be displayed. If the div doesn’t exist, you’ll need to add it in your HTML like this: