O'Reilly logo

JavaScript® Bible, Seventh Edition by Paul Novitski, Danny Goodman, Tia Gustaff Rayl, Michael Morrison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix D. Answers to Tutorial Exercises

This appendix provides answers to the tutorial exercises that appear in Part II of this book (Chapters 5 through 14).

Chapter 5 Answers

  1. The paragraph in Listing 5-1 was originally:

    <p>It is currently <span id="output">now</span>.</p>

    The JavaScript code modifies the contents of the span element because, and only because, it has the ID of output. Therefore, we can get the script to replace the contents of the entire paragraph simply by shifting the ID to the p tag:

    <p id="output">It is currently <span>now</span>.</p>

    The span element is no longer useful:

    <p id="output">It is currently now.</p>
  2. The JavaScript does not need to change in order to reflect this change to the HTML. The steps it performs are as follows:

    1. If there is a first child element,

    2. remove the first child element,

    3. then return to step a.

    Notice how versatile and re-purposeable the deletion loop is. It could easily be made into a generic function to delete the contents of any DOM element.

  3. The style sheet does not have to be changed to assign the paragraph the same font attributes that the span had before, because the styling refers to the element's ID and not its tag name:

    #output
    {
       font-style: italic;
       color: #C33;
    }

    Here again, the CSS rule is not overly specific (it did not point to span#output but merely #output) and therefore is easily re-purposeable even as the HTML undergoes modest change.

Chapter 6 Answers

  1. The catalog page (a) and temperature calculator (d) are good client-side JavaScript ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required