View Full Version : Using arrays to manipulate results of check boxes in a form.

03-02-2012, 05:16 PM
Hello, I am very new to javascript and coding in general. I had a few programming courses 10 years ago in engineering school but never stuck with it.

My larger goal is to create a website with 20 check boxes which the user can select from. When they hit the submit button, the java script will read which boxes are selected do a probability calculation (each of the 20 options will multiply a variable by a different percent and the end result will be displayed.

As I said my coding is limited and by looking at other code on websites I liked and reading a few tutorials I have came up with a working html form and JS that uses the following two functions to read and display my results.

function getSelected(opt) {
var selected = new Array();
var index = 0;
for (var intLoop = 0; intLoop < opt.length; intLoop++) {
if ((opt[intLoop].selected) ||
(opt[intLoop].checked)) {
index = selected.length;
selected[index] = new Object;
selected[index].value = opt[intLoop].value;
selected[index].index = intLoop;
return selected;

function outputSelected(opt) {
var sel = getSelected(opt);
var strSel = "";
for (var item in sel)
strSel += sel[item].value + "\n";
alert("Selected Items:\n" + strSel);

Here is an example of one of the form checkbox elements

<p><input type="checkbox" name="chkboxarray" value="C" id="C"> : <label for="cb2">C<br></label></p><TD>

the function is called from the form button as


Now that I have given you the background information. I am trying to read the results of the form and do the probability calculation. My basic logic is
if ? is checked then multiply prob by x
I was thinking of doing this for all 20 values.

Again I am very new to this, but I think this should work

if (selected.indexOf(C) != -1) {
prob = prob * 0.20 }

but the debugger keeps telling me that 'selected" is undefined.

I can't find the issue I am new to arrays . Is there a better more efficient way?



P.S. If it is helpful I can post the entire code including the HTML, just let me know.

03-02-2012, 05:26 PM
Post the code that is returning your error.

03-02-2012, 05:46 PM
Thank you for the reply

function probability() {
var prob = 1;
if (selected.indexOf(C) != -1) {prob = prob * 0.20 } ;
alert(+ prob)}

This is the problem function it returns "selected is undefined'

selected is defined in the getSelected function
Do I need to bury this code into getSelected?

Again, thanks for the reply


Old Pedant
03-03-2012, 12:08 AM
A variable declared inside a function is *ONLY* available within that function.

To make it available to other code, move the declaration outside of any function.

ALSO: The indexOf method is only applicable to strings. Your selected variable is not a string. See below.

<script type="text/javascrip"> <!-- language= is obsolete -->
var selected = null; // delaration is now page scope

function getSelected(opt) {
selected = new Array(); // **OMIT** the var keyword here!
var index = 0;

function probability() {
if ( selected == null )
alert("you must push the submit button first");
var prob = 1;
if (selected[C] != -1) {prob = prob * 0.20 } ;

03-03-2012, 12:23 AM
Thanks Old Pendant

I will tinker with this awhile and let you guys know how it goes.

Much appreciate the time and guidance.