1. Home
  2. Computing & Technology
  3. JavaScript

Points Distribution Script

3. Calling the Script

Join the Discussion

Questions? Comments?

More of this Feature

What it Does Adding the Script

Now that we have set up the script and attached it to our web page, all that remains is to put the necessary fields into our form and set them up to work with the script.

The first field we need to add to our form is one to display the number of points that are remaining. The code for this field looks like this:

<input type="text" name="remain" size="5" value="50" />

This field must be named remain to work with the script and it should be assigned the same value as you gave maxpts or it will look a bit strange when the points are adjusted when the first option is selected.

We also need to add the check boxes for the individual options that are available to be selected. Each of these should be coded similarly like this:

<input type="checkbox" name="opt0" onchange="ptAdjust(myform,this);" />

These fields need to be named opt0, opt1, opt2, etc. with the number of points required to select the options being the same as the respective value assigned to pt[selbx++] (the array entries start from 0).

The actual script functioning is called from the onchange event code and the name associated with the form needs to be passed as the first parameter.

With these codings in place, whenever any of the check boxes is checked or unchecked the total number of points for all of the checked boxes will be recalculated by the script and if insufficient points are available then the last box checked will be unchecked again and the 'insufficient points' message displayed.

What it Does | Adding the Script | Calling the Script
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.