Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  194] [ 2]  / answers: 1 / hits: 15648  / 13 Years ago, sat, january 14, 2012, 12:00:00

Why doesn't this code work? I'am using FF.



<head>
<script type=text/javascript>

document.getElementById(someID).onclick = function(){
alert(Yahooo);
}
</script>
</head>

<body>
<a href=# id=someID>someID</a>
</body>

</html>


I'm getting javascript error getElementById equals to null.


More From » html

 Answers
7

The needed DOM is not loaded when the script is executed. Either move it down (below the href) or define it like this:



window.onload = function () {
document.getElementById(someID).onclick = function(){
alert(Yahooo);
}
}


window.onload will be called when the page is completely loaded.


[#88016] Friday, January 13, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;