Improve Site Performance Increase Site Traffic Monitor Site Uptime Webmaster Resources NetMechanic Home Looking For Help? Partner Programs Privacy Policy Contact Us Press Room
NetMechanic Home LOGIN | HELP | ABOUT US | PRODUCTS | SITE MAP
NetMechanic Menu
Over 52 Million Web Pages Tested!     
 
Positioning tips.
search engine optimization story search.
Search for:


Your Email:

I would like to receive my newsletter in:
HTML format
Text format


Increase traffic.
Volume 8 (2005)
   September
   June
   April
   March
   January

Volume 7 (2004)
   November
   September
   July
   June
   May
   April
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 6 (2003)
   December
   November (Part 2)
   November
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June (Part 2)
   June
   May (Part 2)
   May
   April (Part 2)
   April
   March (Part 2)
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 5 (2002)
   December (Part 2)
   December
   November (Part 2)
   November
   October (Part 2)
   October
   September (Part 2)
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June (Part2)
   June
   May (Part 2)
   May
   April (Part 2)
   April
   March (Part 2)
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 4 (2001)
   December (Part 2)
   December
   November (Part 2)
   November
   October (Part 2)
   October
   September (Part 2)
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June (Part 2)
   June
   May (Part 2)
   May
   April (Part 2)
   April
   March (Part 2)
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 3 (2000)
   December (Part 2)
   December
   November (Part 2)
   November
   October (Part 2)
   October
   September (Part 2)
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June
   May
   April
   March
   February
   January

Volume 2 (1999)
   December
   November
   October
   September
   July
   June
   May
   April
   March
   February
   January

Volume 1 (1998)
   December
   November
   October
   September

 

JavaScript Tip:
Limit Visitors' Choices

by Larisa Thomason,
Senior Web Analyst,
NetMechanic, Inc.

  
July 2004
Vol. 7, No. 9
 • Design Tip
 • JavaScript Tip
 • Beginner Tip
  

Suppose you're offering a special promotion of two free items with every large purchase. Customers select the two items they want from a list of choices on your shopping cart form. But what should you do if a customer selects more than two items from the list? Avoid that problem by including this simple JavaScript function that limits visitors' choices to only two selections.

The Basic Form With No Limits

Although a shopping cart form is generally pretty complex, we're going to keep this example simple so you can focus on what's happening with the checkboxes instead of getting confused by the larger form.

Here's the basic form code:

<form method=""  action="" name="checkout">
<p>Congratulations!  Your order of $534 qualifies
  you for two free items.  Please select <strong>
  two items only</strong> from the list below:</p>

<div style="border-color:navy; border-style:solid; 
  border-width:2px;width:300px;padding:5px;">
<input type="checkbox" id="armadillo">Imitation 
  Armadillo-skin wallet<br>
<input type="checkbox" id="jalapeno">Jalapeno 
  toothbrush cover<br>
<input type="checkbox" id="seder">Small chocolate 
  Seder plate<br>
<input type="checkbox" id="treat">Cousin 
  Christine's Horse Treats (bag of 6)<br>
<input type="checkbox" id="laces">2 sets of lemon-
  yellow boot laces<br>
</div>

</form>

That creates a basic form that looks like this:

Congratulations! Your order of $534 qualifies you for two free items. Please select two items only from the list below:

Imitation Armadillo-skin wallet
Jalapeno toothbrush cover
Small chocolate Seder plate
Cousin Christine's Horse Treats (bag of 6)
2 sets of lemon-yellow boot laces

Adding Form Controls With JavaScript

If the customer isn't paying close attention or just can't choose between the fabulous gifts, it's easy to select more than two items in the above form. What would you do if your customer selected all five instead of the two she was entitled to?

You could choose for her, but what if you send the toothbrush cover when she really wanted to boot laces more? You could also contact her directly, but that delays the order processing and consumes more of your time.

The best option is to ensure that customers can only select two items. Use a gentle reminder in a JavaScript alert box to remind them. Add this function to the HEAD section of your page:

What's happening in this script? Let's review it one step at a time:

  • var freeItemCount=0: Sets the initial count of items selected to zero.
  • var maxFreeItems=2: Sets the maximum number of choices to two. Note: It can be set to any number that meets the needs of your application, but we're using two as an example.
  • function setItems(item): This function keeps track of the number of items selected. The function has three components:

    1. Each time the customer clicks on a checkbox, the function adds one to the freeItemCount total.
    2. The freeItemCount total is reduced by one when the customer unclicks a checkbox.
    3. If your customer selects more than two items, the function unchecks the third selection, reduces the freeItemCount total by one, and displays an alert box to remind the customer that she's only entitled to two free items.

Call the function inside the INPUT tag for each checkbox. This is an important step! Without a function call, the script won't work.

<form method=""  action="" name="checkoutLimited">
<p>Congratulations!  Your order of $534 qualifies
  you for two free items.  Please select <strong>
  two items only</strong> from the list below:</p>

<div style="border-color:navy; border-style:solid; 
  border-width:2px;width:300px;padding:5px;">
<input type="checkbox" id="armadillo" 
  onclick="setItems(this)">Imitation Armadillo-
  skin wallet<br>
<input type="checkbox" id="jalapeno" 
  onclick="setItems(this)">Jalapeno toothbrush 
  cover<br> 
<input type="checkbox" id="seder" 
  onclick="setItems(this)">Small chocolate Seder 
  plate<br> 
<input type="checkbox" id="treat" 
  onclick="setItems(this)">Cousin Christine's Horse 
  Treats (bag of 6)<br> 
<input type="checkbox" id="laces" 
  onclick="setItems(this)">2 Sets of lemon-yellow 
  boot laces<br> 
</div>

</form>

Here's how it works on the page. Select three items and see if you get the alert box.

Congratulations! Your order of $534 qualifies you for two free items. Please select two items only from the list below:

Imitation Armadillo-skin wallet
Jalapeno toothbrush cover
Small chocolate Seder plate
Cousin Christine's Horse Treats (bag of 6)
2 Sets of lemon-yellow boot laces

Careful With Netscape Issues

Customers must have JavaScript turned on in their browsers for this script to work - but most shopping cart scripts also require JavaScript so you aren't locking out any customers by using the function.

The script works fine in Explorer, Opera, and Netscape 7, but watch for potential problems with Netscape 6.x browsers. Customers using Netscape 6.x who select more than two items receive the error message - but only the first time! After the first error message, the customer is free to select more than two items with no reminders.

This is small example of how browser differences can cause unexpected problems for webmasters. Browser differences are much more critical when they involve Web page layout and display. Always test your pages with Browser Photo. It shows you screen shots of your Web page in 16 different browser, browser versions, and operating system combinations. You'll find and fix display problems before your visitors complain about them.



Rate This Tip:
Not Useful Useful Very Useful   
 
NetMechanic Tools
HTML Toolbox
Browser Photo
Server Check
Search Engine Starter
Search Engine Tools
GIFBot
Newsletter
HTML Tutorial and Tips
Search Engine Tutorial
Accessibility Information
Browser Problem Tutorial

Company Info
Products
About Us
Contact
Advertise
Link To Us
Jobs
Privacy Policy
Partner Programs
Press Room
RSS Feed
Support
 



Powered by Overture!

 
     
 
   
 
     


Keynote Home
Copyright © 1996-2007,
Keynote NetMechanic
All rights reserved.