[Solved] How to use html elements in if statement?


There are multiple issues going on here:

  • You use strict equality === where you attempt to compare a string and a number
  • You attempt to access .value on a non-input element, so numPage is undefined

Solution:

<button id="last-page" onclick="getNum()">last page</button>
<script>
  function getNum() {
    var numPage = document.getElementById("page-num").textContent;

    if (numPage == 1) {
      console.log("matches");
    } else {
      console.log("doesn't match");
    }
  }
</script>
<div id="page-num">1</div>

0

solved How to use html elements in if statement?