Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  139] [ 4]  / answers: 1 / hits: 47884  / 11 Years ago, thu, june 27, 2013, 12:00:00

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..


More From » html

 Answers
36

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


[#77378] Wednesday, June 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dayshadelaniej

Total Points: 668
Total Questions: 121
Total Answers: 121

Location: Sao Tome and Principe
Member since Wed, Dec 21, 2022
1 Year ago
;