skip to Main Content

is JavaScript code in google
Error: Cannot read properties of null (reading ‘style’)

document.getElementById('splash').style.display = 'none'

Error: Cannot read properties of null (reading ‘style’)

                document.getElementById('splash').style.display = 'none'
        console.log("Already shown");
      }
    }

enter image description hereIt shows an error in this code, the code

Uncaught Type Error: Cannot read properties of null (reading ‘style’)

document.getElementById('splash').style.display = 'none'

enter image description here

2

Answers


  1. Try this:

    var elem = document.getElementById("splash");
    elem.style.display= 'none';
    

    does your element have class name?

    Login or Signup to reply.
  2. If your element does truly exist, then the problem is most likely that you are referencing your js file in a <script> tag in the body of your html before the element itself. You need to make sure that the script tag is placed at the complete bottom of the <body> tag after the element you have defined. Another way is to put the script tag in the html head tag and add a simple property of defer="true". Lastly, if you do not want to do that, simply in your js just do a window.onload = () => { //write all your code here } and with that function when you console.log() the element, it would have loaded.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search