On Mon, 6 Jun 2016 11:22:13 -0700 (PDT), Mohit Bajoria wrote:
> Hello developers
> There is a simple game in which after one onclick event is fired, second one is not working
> I am pasting the code and u can use any image with name 'smile.png'
[snip]
This line has 3 errors:
document.getElementsByTagName("body").onload = generateFaces();
1. Keep in minds that getElementsByTagName() is to get elements, not
element. So the correct code should have been:
document.getElementsByTagName("body")[0] //...
2. A HTML page may only have one BODY element, so there's no point on using
getElementsByTagName(), especially since there's document.body already
available. Thus, it would be better like this:
document.body //...
3. The onload property is assigned with the return value of generateFaces(),
not the reference to the function itself. So...
document.body = generateFaces;
However, the code below generateFaces() which is executed immediately,
refers to theLeftSide.lastChild which won't exist until generateFaces() has
been executed, that code will fail. So, wrap the remaining code in another
function (e.g. init() ) and use it as the onload handler instead of
generateFaces(). Move generateFaces() execution into the start of init()
instead.
Full fixed code:
<
http://pastebin.com/g73PRTnz>