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..
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With