Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot set property 'className' of null

Following code shows error "Uncaught TypeError: Cannot set property 'className' of null"

  <style>
      .deactive { height:250px; }
      .active { height:150px; }
  </style>
  <script>
      function click(div) {
          document.getElementById('div').className  = 'active';
      }
  </script>
  <a href="#" onclick="click(Division);">
      <div id="Division" class="deactive">
          <!--Division contents-->
      </div>
  </a>

i am trying pass id of division to js function by on click event of anchor tag.. and i want to change the class name of the division, help me to identify the error..

like image 473
chriz Avatar asked Dec 26 '22 01:12

chriz


1 Answers

That error occurs because in your document you have no elements whose id is "div" so document.getElementById('div') is null.

You need to change here:

onclick="click('division');"

use quotes around argument, and

document.getElementById(div).className  = 'active';

div here must not be enclosed in quotes, since you're using the parameter passed when the function has been called

(as a side note: avoid to call your function click)

example fiddle

like image 103
Fabrizio Calderan Avatar answered Jan 04 '23 17:01

Fabrizio Calderan