Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  115] [ 7]  / answers: 1 / hits: 20870  / 11 Years ago, thu, april 25, 2013, 12:00:00

see below is my javascript,



<script type=text/javascript>
document.getElementById('auth').onclick=change;
function change(){
this.className=account_holder;
}
</script>


html is



<td id=auth class=authorised_reportericon >
<a href={% url incident.views.authorised_reporter %}>
<p align=center style=color:black;font-size:16px;>Authorised Reporters</p></a>
</td>


I assigned td's id for onclick,but it is trowing error as Uncaught TypeError: Cannot set property 'onclick' of null


More From » html

 Answers
303

Put document.getElementById('auth').onclick=change; inside of window.onload, like:



window.onload = function () {
document.getElementById('auth').onclick=change;
};


According to the error you're getting, I'm guessing your Javascript is executed before your relevant HTML is rendered, meaning the element with the id auth isn't found. Putting it in an event where the whole DOM is ready should fix the problem.



Demo: Here


[#78622] Wednesday, April 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryder

Total Points: 473
Total Questions: 110
Total Answers: 91

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
;