-2

I've the following code :

var exit = document.getElementById("exit");
exit.onclick = function() {
   "use strict";
    document.getElementById("fadedDiv").style.display = "none" ; 
};

But in the console I get the following error:

script.js:107 Uncaught TypeError: Cannot set property 'onclick' of null

Although it worked well with me before, but I don't know what the problem is.

plz help me

James Jones
  • 3,632
  • 5
  • 21
  • 41
  • 3
    Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Andreas Aug 21 '18 at 11:44
  • var exit = document.getElementById("exit"); probably doesn't resolve to any element within your webpage. Try to do a console.log(exit) before u assign the onclick and see in chrome developer console what it outputs. – Jonas B Aug 21 '18 at 11:44
  • document.getElementById("exit") returns a null when there is no element with id="exit" – Ashish Jhanwar Aug 21 '18 at 13:16

1 Answers1

0

Because in your HTML you gave the parent Div a class name of "fadedDiv", then you try to use

document.getElementById("fadedDiv")

You can change the parent div tag to be an id

 <div id="fadedDiv"> 
      this is div
      <div id="exit" >&#10005; text</div> 
</div>
Majid Eltayeb
  • 450
  • 3
  • 14