HTML5, CSS3, and JavaScript Fourth Edition

(Ben Green) #1

CHAPTER 14. JAVASCRIPT AND AJAX 154


JavaScript Details:


Thedocumentobject is the entire document.


ThegetElementByIdfunction searches the document for an object that
has the specified ID, which in this case isabc.


ThehiddenDOM attribute specifies whether the object is to be hidden or
revealed.


14.6 Try It: Make a Function


Functions let us simplify our webpage.


The onclick= html can get rather verbose. You can shorten it considerably
by creating a JavaScript function.


In the body.style.color example, we used JavaScript to immediately modify
an attribute of body. We are doing exactly one thing.


Sometimes we want to do several things all at once. We can put those
things into a JavaScript function and give it a name. Then, in the onclick=
html we can simply mention the name of the function and all those things
will happen. This can greatly simplify your html, plus it collects all the
JavaScript into one place.


Warning: The name you give to your JavaScript function must be different
than any of the IDs you have on your webpage.


In this example, we create a function namedcolorsthat does several steps,
and we call it with two parameters, the foreground (text) color, and the
background color. Inside the function, we change both colors.



[White/Red]
[Red/White]
[Red/Blue]

Free download pdf