I have a doubt about the JavaScript element assignment. Here is my code for the "save" function. May I know why the code below shows error:
Cannot read properties of null (reading 'textContent')
let saveEl = document.getElementById("save-el");
function save(){
let countStr = count + " - ";
saveEl.textContent += countStr //This line doesn't worked.
console.log(count);
}
But, when I removed the variable assignment saveEl as shown below, the code works well.
function save(){
let countStr = count + " - ";
document.getElementById("save-el").textContent += countStr;
console.log(count);
}
Can anyone here explain to me why my code behave this way?