ws.generate();
getElement("eq0-label").innerHTML = ws.getEquation();
getElement("score").innerHTML = score;
};
};
Checking whether the answer was correct gave us qu ite a headache. Initially, we used
the Number() function to convert the answer f rom string to number. The score was
sometimes increased even thoug h w e entered no answer. Then we realized that an
empty string is, of course, converted to zero . So we u sed parseInt() instead.
Professor: Excellent! You did a great job. I believe that everything else is q uite
obvious and we’re ready to move on.
13.2 Using Family Relations to Manipulate Elements
Professor: Unlike the Kubla Khan poem, wh ic h was an example of a dynamic docu-
ment, the math worksheet generator is a web application. It is therefore not necessary
that we limit ou rselves to a fixed number of equations. However, to be able to p ro-
grammatica lly set the number of equations that are displayed within the worksheet,
you should know some more about the DOM manipulation methods that JavaScript
has in store for you.
Recall whe n we discussed family relations of HTML elements, and how we repre-
sented those relations in the form of a family tree. If you want, you can refresh your
memory by looking at the tree o n page 61. It will come as no surpr ise to you that
DOM objects—because they represent HTML elements—reflect exactly the same re-
lations within a tree, and you can refer to these objects in terms of relations like child
or sibling as w ell. The DOM API is in fact quite complex but you don’t need to know
much to be able to start using it to your advantage. The important thing to know is that
every HTML element is represented by an Element object, which is a subtype of the
Node class. What that mean s in practice is that you can use methods and properties of
the Node as well as those of the Element class when working with HTML elements.
You have already learned how to get hold of an HTML elemen t using the getElement
ById() method of the Document object and manipula te it using the innerHTML prop-
erty, or properties that mirror HTML attributes. You also learned that you can define
event handlers for eleme nts, and you can even insert other elements inside existing
ones dynamically by simply assigning appropr ia te HTML code to innerHTML. You
can do some pretty amazing stuff with all that already but sooner or later you’ll want
more flexibility.
While you can grab an element by its ID, this may not be very convenient if you
want to get more elements tha t logically belong together and to manipulate them all
in the same way. This is exactly what we need in our math worksheet generator as
soon as we decide to include more than one equation into the worksheet. One way
of getting several elements at the same time is fetc hing elements by their names. You
can do this with the getElem entsByTagName() method of an Element or D ocument
object. Since the Document object is the root object of DOM, invoking this method
13.2. Using Family Relations to Manipulate Elements 247