spans and getElementsByTagName()

If each F were in a div of its own, this JavaScript could be rewritten so that clicking an individual F would make it bigger. Unfortunately, browsers insert line breaks before and after each div, so the text would look like Figure 13-13. To mark a bit of HTML without introducing line breaks, use the <span> tag. A span is an HTML element that differs from a div only in that it doesn't create line breaks.

Figure 13-14 shows how to use spans. It also introduces the built-in JavaScript method document.getElementsByTagName(), which returns an array of all the HTML elements of a given kind on the web page.

Putting Fs inside <div> tags

Figure 13-13. Putting ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.