0

I know only very little about JavaScript so far but I wanted to implement a small script which I thought to sound quite simple. I have a line in a HTML document saying (translated) "Has written X posts" and basically I would like it to just say "post" at the end when that number is 1.

Here's what I got so far. My editor is already showing me that something is wrong with the script, though on my own I haven't really been able to figure out what. Little help would be appreciated :')

<html>
Hat insgesamt <span id="zahl">29</span> <span id="spuren">Spuren</span> hinterlassen.
</html>

<script>
let spur;
if (document.getElementById("zahl").innerHTML) = 1 {
spur = "Spur";
} else {
spur = "Spuren";
}

document.getElementById("spuren").innerHTML = spur;
</script>
pilchard
  • 12,414
  • 5
  • 11
  • 23
Indasa
  • 1
  • 2
    you have a typo: `(document.getElementById("zahl").innerHTML) = 1` is not valid. Also `=` is used for assignment while `===` is used for comparison, so it should read: `(document.getElementById("zahl").innerHTML === 1)` – pilchard Sep 01 '23 at 13:43
  • 2
    Also, when comparing text content of elements use [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) instead of [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) as it is cheaper – pilchard Sep 01 '23 at 13:45
  • And you should compare the same type. `textContent` is a string, so compare with `"1"`. – Heretic Monkey Sep 01 '23 at 13:46
  • see: [What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)](https://stackoverflow.com/questions/11871616/what-is-the-difference-between-the-and-operators-and-what-is-si) – pilchard Sep 01 '23 at 13:46

0 Answers0