0

I am trying to make small application which uses some html checkbox form objects. Whenever any of those checkboxes are checked on or off, it runs a function which then changes the value of a variable unique to each checkbox. How would I go about making it so that using the arguments of the function - or some other means - I'm able to target the corresponding variable for whenever one of the checkboxes is clicked? Currently here's what I'm attempting to accomplish.

HTML:

 <form action="" method="get" name="orderForm">
<input name="Test" type="checkbox" value="test" onchange="checkClick('1','5.00')" />Test 1<br />
<input name="Test" type="checkbox" value="test2" onchange="checkBoxClick('2','3.00')" />Test 2 <br />
<input name="Test" type="checkbox" value="test3" onchange="checkBoxClick('3','4.00')" />Test 3 <br />

And the Javascript:

//set global vars
var check1 = 0.00;
var check2 = 0.00;
var check3 = 0.00;

function checkClick(num,cost) {
    if (check+num == 0.00) {
        check+num = cost;
    }
    else {
        check+num = 0.00;
    }
    checkBoxClick();
}

function checkBoxClick() {
    document.getElementById("otherContainer").innerHTML = "<p>" + check1 + "<br />" + check2 + "<br />" + check3 + "<br />" + check4 + "</p>";
}

At the moment it's just a crude test, but what I'm trying to do is make it so I only need one function that will handle each of the different checkboxes and their variables without having to make a different function for each one.

3 Answers 3

4

I see what you're trying to do. Make a variable check an array instead:

var check = [ 0.00, 0.00, 0.00 ];

Also, pass your parameters as numbers instead:

checkClick(1, 5.00)

But since arrays are 0-indexed, start with 0 instead of 1:

checkClick(0, 5.00)

And where ever you did check+num replace it with check[num].

Here is the result:

var check = [ 0.00, 0.00, 0.00 ];

function checkClick(num, cost) {
    if (check[num] == 0.00) {
        check[num] = cost;
    } else {
        check[num] = 0.00;
    }
    checkBoxClick();
}

function checkBoxClick() {
    document.getElementById("otherContainer").innerHTML = "<p>" + check[1] + "<br />" + check[2] + "<br />" + check[3] + "<br />" + check[4] + "</p>";
}

BTW, you're using check4 when you never made a check4 variable...

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh beautiful! Making it an array instead completely slipped my mind, thank you very much!
0

Use an array instead of three separate variables. Then modify the array element based on the number you are passing to the function.

Better still, try using java objects with keys as the id of the checkbox.

Comments

0

En HTML:

<input name="Test" type="checkbox" value="test" onchange="checkClick(this,'5.00')" />Test 1<br />
<input name="Test" type="checkbox" value="test2" onchange="checkClick(this,'3.00')" />Test 2 <br />
<input name="Test" type="checkbox" value="test3" onchange="checkClick(this,'4.00')" />Test 3 <br />

En Javascript:

function checkClick(element,cost) {
   switch (element.value) {
      case 'test':
         check1 = (element.checked) ? cost : 0;  
         break;
      case 'test2':
         check2 = (element.checked) ? cost : 0;  
         break;
      case 'test3':
         check3 = (element.checked) ? cost : 0;
         break;
      }
      checkBoxClick();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.