0% found this document useful (0 votes)
16 views

How To Program - Chapter2 - The Colour

The document describes a JavaScript script that changes the background color of a web page. The script defines a function called changecolor(code) that sets the document's bgColor property equal to the code parameter. Buttons in an HTML form call this function on click, passing in color values like 'red' or 'green' to change the background color. This allows the single changecolor function to be reused to change the color when any button is clicked.

Uploaded by

James Viviers
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

How To Program - Chapter2 - The Colour

The document describes a JavaScript script that changes the background color of a web page. The script defines a function called changecolor(code) that sets the document's bgColor property equal to the code parameter. Buttons in an HTML form call this function on click, passing in color values like 'red' or 'green' to change the background color. This allows the single changecolor function to be reused to change the color when any button is clicked.

Uploaded by

James Viviers
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 2

Here is a look at the script. See if you can trace how it works.

<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -

function changecolor(code) {

document.bgColor=code
}

// - End of JavaScript - -->


</SCRIPT>

</HEAD>
<BODY>

<form>
<input type="button" name="Button1" value="RED" onclick="changecolor('red')">

<input type="button" name="Button2" value="GREEN" onclick="changecolor('green')">

<input type="button" name="Button3" value="BLUE" onclick="changecolor('blue')">

<input type="button" name="Button4" value="WHITE" onclick="changecolor('white')">

</form>

</BODY>
</HTML>

function changecolor(code) {
document.bgColor=code
}

This page is an excellent example of one function being used by several buttons in the same
document. Our function is called changecolor(code). It has only one line:
document.bgColor=code.

You probably guessed that bgColor stands for background color.


If I type document.bgColor="red" the background will turn red.
Chapter 2

By writing the command document.bgColor=code I am allowing myself to define the variable


code as any color I want.

Later, in the form. I define a button with the command:


onClick="changecolor('green')"

onClick="changecolor('green')" is telling the program 2 things:

1. Run the funtion named changecolor(code).


2. code='green'

You might also like