SHARE
Facebook X Pinterest WhatsApp

JavaScript Primers #17

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

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

F

ollow

this link

to see the script in action



Deconstructing the Script

  • Time for a new term! A literal is a VALUE that does not change. It can be a number, a name, or any random series of both. Just remember that a literal is solid. It cannot be altered.

  • Time for another new term! A string is any run of letters or numbers within single or double quotes. Thus this section from the script:
    onClick=”newcolor(‘lightblue’)

    …is defining the literal string “lightblue.” Still with me? Good.

  • Here’s the script and the input items again:
  •     function newcolor(color)
        {
            alert("You chose " + color)
            document.bgColor=color
        }
    
    <form>
     <INPUT TYPE="button" VALUE="blue" 
         onClick="newcolor('lightblue')">
     <INPUT TYPE="button" VALUE="Pink" 
         onClick="newcolor('pink')">
    </form>
    
  • Notice we are passing a literal string, ‘lightblue’ or ‘pink’, to the function, newcolor(). The string is surrounded by single quotes because the function name is surrounded by double quotes.
  • When a button is clicked, the string in the parentheses
    (either ‘pink’ for ‘lightblue’) is passed to the function newcolor().
  • Basically, the function is waiting until it is given the information it needs to perform. Remember that in all functions up until this point, the parentheses were empty. The function had all the parts it needed. Here it does not and it won’t until someone
    clicks on a button. That button contains the same function, only this time the function does have the data it needs, a color command.
    In this script that color command is being passed onto two items in the <SCRIPT> section: an alert method and the document.bgColor section. After the function has what it needs, it acts. The alert box pops up and the background color changes.
  • I want to point out, because it can be confusing, that the VALUE attribute in the INPUT command puts the text on the button. It doesn’t affect the JavaScript properties.


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

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.