The Essential Guide to HTML5

(Greg DeLong) #1

CHAPTER 9


Code Explanation


for(i=0;i<an;i++) { Iterates to create alphabet buttons


uniqueid = "a"+String(i); Creates a unique identifier.


d = document.createElement('alphabet'); Creates an element of type alphabet


d.innerHTML = ( Defines the contents as specified in the next
line


"<div class='letters'
id='"+uniqueid+"'>"+alphabet[i]+"

");


Specifies a div of class letters with a
unique identifier and text content, which is the
ith letter of the alphabet

document.body.appendChild(d); Adds to body


thingelem = document.getElementById
(uniqueid);


Gets the element with the id

x = alphabetx + alphabetwidth*i; Computes its horizontal position


y = alphabety; Sets the vertical position


thingelem.style.top = String(y)+"px"; Using the style top, sets the vertical position


thingelem.style.left = String(x)+"px"; Using the style left, sets the horizontal
position


thingelem.addEventListener('click',
pickelement,false);


Sets up event handling for the mouse click
event

} Closes the for loop


var ch = Math.floor(Math.random()*
words.length);


Chooses, at random, an index for one of the
words

secret = words[ch]; Set the global variable secret to be this word


for (i=0;i<secret.length;i++) { Iterates for the length of the secret word


uniqueid = "s"+String(i); Creates a unique identifier for the word


d = document.createElement('secret'); Creates an element for the word

Free download pdf