John
John

Reputation: 33

help with error SCRIPT5007: Unable to set value of the property 'onclick'

I have a link in the html page and it has the id="redirect"

and this in (script.js)

window.onload = initAll();

function initAll(){
    document.getElementById("redirect").onclick = clickHandler;
}
function clickHandler(){
    alert("Sure!");
    return true;
} 

I'm getting this error message

SCRIPT5007: Unable to set value of the property 'onclick': object is null or undefined 

I tried this in (IE9,IE8,chrome,Firefox 4.0.1) and still not running, please help

Upvotes: 3

Views: 7059

Answers (1)

DanielB
DanielB

Reputation: 20230

You have to write:

window.onload = initAll;

At the point of execution the document element does not exists, because you don't call the function on load. You call the initAll() and assigning its return value to window.onload which is undefined or raising the error.

Upvotes: 4

Related Questions