Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Workshop: Creating Dynamic Styles

Using the DOM style objects, you can create a page that allows you to directly control the colors used in the page's text. To begin with, you will need a form with which to select colors. The following is a basic form using <select> tags to define options for head and body colors:

<FORM NAME="form1">
<B>Heading color: </B>
<select name="heading" onChange="changehead()">
   <option value="black">Black</option>
   <option value="red">Red</option>
   <option value="blue">Blue</option>
   <option value="green">Green</option>
   <option value="yellow">Yellow</option>
</select>
<br>
<B>Body text color: </B>
<select name="body" onChange="changebody()">
   <option value="black">Black</option>
   <option value="red">Red</option>
   <option value="blue">Blue</option>
   <option value="green">Green</option>
   <option value="yellow">Yellow</option>
</select>
</FORM>

Notice that this form uses onChange attributes in the <select> tags to call two functions, changehead and changebody, when their respective selection changes. Next, you will need to create these functions. The following script defines the style changing functions:

<script language="Javascript" type="text/javascript">
function changehead() {
  i = document.form1.heading.selectedIndex;
  headcolor = document.form1.heading.options[i].value;
  document.getElementById("head1").style.color = headcolor;
}
function changebody() {
  i = document.form1.body.selectedIndex;
  doccolor = document.form1.body.options[i].value;
  document.getElementById("p1").style.color = doccolor;
}
</script>

This script first defines the changehead function. This reads the index for the currently selected heading color, then reads the color value for the index. This function uses the getElementById method described in the previous section to change the color. The changebody function uses the same syntax to change the body color.

Last but not least, you will need some basic HTML, including a heading and some body text to be changed by the functions. Listing 18.2 shows the complete HTML document, including the script and form you created earlier.

Example 18.2. The complete dynamic styles example

<HTML>
<HEAD>
<TITLE>Controlling Styles with JavaScript</TITLE>
<script language="Javascript" type="text/javascript">
function changehead() {
  i = document.form1.heading.selectedIndex;
  headcolor = document.form1.heading.options[i].value;
  document.getElementById("head1").style.color = headcolor;
}
function changebody() {
  i = document.form1.body.selectedIndex;
  doccolor = document.form1.body.options[i].value;
  document.getElementById("p1").style.color = doccolor;
}
</script>
</HEAD>
<BODY>
<h1 ID="head1">
Controlling Styles with JavaScript</h1>
<hr>
<p ID="p1">
Select the color for paragraphs and headings using the form below.
The colors you specified will be dynamically changed in this document.
The change occurs as soon as you change the value of either of the
drop-down lists in the form.
</p>
<FORM NAME="form1">
<B>Heading color: </B>
<select name="heading" onChange="changehead()">
   <option value="black">Black</option>
   <option value="red">Red</option>
   <option value="blue">Blue</option>
   <option value="green">Green</option>
   <option value="yellow">Yellow</option>
</select>
<br>
<B>Body text color: </B>
<select name="body" onChange="changebody()">
   <option value="black">Black</option>
   <option value="red">Red</option>
   <option value="blue">Blue</option>
   <option value="green">Green</option>
   <option value="yellow">Yellow</option>
</select>
</FORM>
</BODY>
</HTML>

Notice that the <h1> tag has an ID attribute of "head1", and the <p> tag has an ID of "p1". These are the values the script uses in the getElementById function.

To test the dynamic styles script, load Listing 18.2 into the browser. Select the colors, and notice the immediate change in the heading or body of the page. Figure 18.2 shows a typical display of this document after the colors have been changed.

18fig02.jpg

Figure 18.2 The dynamic styles example in action.

Share ThisShare This

Informit Network