JavaScript Primers #17

By Joe Burns

http://www.htmlgoodies.com/primers/jsp/article.php/3478291/JavaScript-Primers-17.htm (Back to article)

Use these to jump around or read it all


The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment


The Concept

The purpose of this example is to introduce you to forms and JavaScript. This script uses a form to allow you to choose a background color, either blue or pink. Notice that the color selection is done through form buttons.

Forms always begin with <FORM> and end with </FORM>. No surprises here, just good old HTML!


The Script

The example below will again show the full HTML document:


<html>
<head>
<SCRIPT type="text/javascript">
function newcolor(color)
{
  alert("You Chose " + color)
  document.bgColor=color
}
</SCRIPT>
</HEAD>
<BODY>
<h1>Select a Background Color</h1>
<FORM>
  <INPUT TYPE="button" VALUE="Blue" 
      onClick="newcolor('lightblue')">
  <INPUT TYPE="button" VALUE="Pink" 
      onClick="newcolor('pink')">
</FORM>
</BODY>
</HTML>


The Script's Effect

Follow this link to see the script in action

Deconstructing the Script


What You Have Learned


Your Assignment

Let's see how well you remember some of the stuff from the other primers. Please rewrite the script above so that when the user enters the page, they are given a prompt that asks for their name. Then, when they click on a button, the alert box reads "Hey (name)! You chose (color)." Good luck.

Here's a possible answer to this assignment
(this will open a new window)


The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment

On To JavaScript Primer #18