I am trying to get a number from a document.getElementById to be read as a real number in a if/else statement using a comparison operator (e.g.: points < 40). Below is an example of what I'm trying to accomplish using w3school's tool to assist.
I understand Javascript does certain things depending on the DataTypes, Operators, and Numbers. Below is the original code I started with, but I have tried different variations, see below with the datatype outcome commented to the right. I used arithmetic to find out what's going on with my element's id number when pulled into the variable and the best outcome is NaN.
How do I get a number within an HTML tag to be recognized by Javascript's document.getElementById script operators?
<!DOCTYPE html>
<html>
<body>
<span id="points">39</span> is <span id="outcome"></span>
<script>
var points = document.getElementById("points"); // [object HTMLSpanElement]
//var points = +document.getElementById("points"); // NaN
//var points = parseInt(document.getElementById("points")); // NaN
//var points = Number(document.getElementById("points")); // NaN
if (points < 40) {document.getElementById("outcome").innerHTML = "LESS THAN 40";}
else {document.getElementById("outcome").innerHTML = "40 or GREATER";}
</script>
</body>
</html>
EDIT: All that was needed was a ".innerHTML" addition to my var (added before the semicolon).
FIXED CODE BELOW:
<!DOCTYPE html>
<html>
<body>
<span id="points">39</span> is <span id="outcome"></span>
<script>
var points = document.getElementById("points").innerHTML; // [object HTMLSpanElement]
//var points = +document.getElementById("points"); // NaN
//var points = parseInt(document.getElementById("points")); // NaN
//var points = Number(document.getElementById("points")); // NaN
if (points < 40) {document.getElementById("outcome").innerHTML = "LESS THAN 40";}
else {document.getElementById("outcome").innerHTML = "40 or GREATER";}
</script>
</body>
</html>