0

I'm making a simple counter with a button. I press the button and it increments, that works correctly. But I'm also trying to create a "Save" feature where you can Save your last count. It's returning with "Cannot set properties of null(setting 'innerText').

let buttonCount = 0
let countEl = document.getElementById("count-el")
let saveEl = document.getElementById("save-el")


function increment() {
  buttonCount += 1
  countEl.innerText = buttonCount
  console.log(buttonCount)
}

function save() {
  let countStr = buttonCount + " - "
  saveEl.innerText = countStr
}
<title>Passenger Counter</title>
<h1>People Entered:</h1>
<h2 id="count-el">0</h2>
<script src="index.js"></script>
<button id="increment-butn" onclick="increment()">INCREMENT</button>
<button id="save-butn" onclick="save()">SAVE</button>
<p id="save-el">Previous Entries: </p>
mplungjan
  • 155,085
  • 27
  • 166
  • 222
  • This code works for me as written. Also, when asking a question containing code, try to use the code snippet tool if possible. See how it was edited, and how the edit contains a working code sample. You will also see that the code sample seems to work as written. – Chris Strickland Feb 11 '22 at 09:54

0 Answers0