Apart from their specific properties and methods, Element objects also have prope rties
that mirror the HTML attributes of cor responding HTML elements. For example,
there exists a title HTML attribute, whic h is mirrored in every Ele ment object as a
property named title. The titl e attribute represents textual info rmation about the
element, which is typically displayed in the form of a tooltip appearing wh enever a
mouse poin te r hovers over the element. You can dynamically set the title attribute
of the above <div> element simply by assigning a text string to the title proper ty
of the myDiv object:
myDiv.title = "Click the text and see what happens.";
The element has thus obtaine d a tooltip.
Mike: Is it also possible to add new elements to a document?
Professor: There’s virtually no limit to what you can do with the document. Th ere ex-
ist specialized methods to add and remove e le ments from the document dynamically,
some of which I will show you later. Nevertheless, you can also use innerHTML for
that purpose. For example, you can insert two paragraphs inside our <div> element
like this:
myDiv.innerHTML = "<p>Number nine</p><p>Number nine</p>";
Maria: Doesn’t that delete the p revious conte nt o f the <div> element?
Professor: As a matter of fact, it does. You can use a concatenation operator to
actually add to th e existing content. For example, this adds the third Number nine
paragra ph to the element:
myDiv.innerHTML += "<p>Number nine</p>";
12.5 Timer Events
Professor: One interesting type of events are events fired by timers. Sometimes you
may want certain portions of your code to execute later or even to execute many times
at regular time in te rvals. For example, you may want to remind yo ur visitor that
the time has expired, or to update a clock on your web page every secon d. There
are two important global methods, set Timeout() and setInterval (), which let
you register a function tha t you want to invoke at a later time on ce or many times.
Although functions registered this way are invoked asynchronously a s a response to
the timer-elapse event, they are registered differently than other event handlers and we
often call them callb acks.
236 Meeting 12. Using JavaScript to Control the Browser