0
// here is the function that iḿ trying to use, i've already tried to use  getElementbyID, but the same error keeps on going.

let sla = document.querySelector('#MathSection')


function mathOn() {
  
  if(sla.classList.contains('show')) {
    sla.classList.remove('show')
  } else {
    sla.classList.add('show')
  }
}

I've already tried getElementbyID, but the same error happens, I've tried to change from section to div, but the same error continues, I don't know how to proceed because I'm learning a short time ago, and I don't know how to manipulate the DOM.

  • 2
    You need to show us your HTML code where you include the script. Perhaps your `script` tag is added before the element? – Spectric Nov 23 '22 at 01:01
  • you need to run your script after the #MathSection is created. If your html is static, there are various ways to do so: put your script at the end of your html body, document.addEventListener("DOMContentLoaded", function(event) {/* your script here*/}), add the defer attribute to your script tag – user3252327 Nov 23 '22 at 01:05
  • 1
    Simply moving the `let sla = ...` line into the `mathOn` function would probably solve this – Phil Nov 23 '22 at 01:06

0 Answers0