0

I have recently started javascript at college and am having a little trouble. I am not Including my code here as i dont want it written for me i just need some pointing in the right direction.

So, I have three checkboxes, each with a different value and i need to add together the values for the selected boxes. I understand how to do this but the way im doing it seems a bit long winded and im sure there is a better way.

Currently i have an if statement checking if box1 box2 and box3 are selected, if they are then they are added, else if box 1 and box2 are selected they are added, else if box1 and box2 are selected they are added and so on.

so basically im checking every combination of what boxes can be selected, one at a time. There must be a more efficient way of achieving this so any tips would be appreciated. Thanks in advance for any help.

7
  • 1
    have you asked your TA? Commented May 16, 2013 at 15:25
  • 1
    Code would be helpful, though I think in this case it is not required. You can just use the values of selected elements if needed, no if required. Commented May 16, 2013 at 15:25
  • You're asking for a code review, nothing wrong with that. Try over here: codereview.stackexchange.com, since here we're a little more into solving code problems. Commented May 16, 2013 at 15:25
  • what do you mean by "added"? Commented May 16, 2013 at 15:26
  • pure js or jquery is ok? Commented May 16, 2013 at 15:26

2 Answers 2

2

Assuming that your HTML resembles the following:

<input name="demo" type="checkbox" checked value="1" />
<input name="demo" type="checkbox" value="2" />
<input name="demo" type="checkbox" checked value="3" />

Then simply retrieve the elements of the relevant name, then iterate through the collection/nodeList and add the values to a declared 'total' variable:

var boxes = document.getElementsByName('demo'),
    total = 0;

for (var i = 0, len = boxes.length; i < len; i++){
    if (boxes[i].checked && boxes[i].type.toLowerCase() == 'checkbox') {
        total += parseInt(boxes[i].value, 10);
    }
}

console.log(total);

JS Fiddle demo.

the above code is, I think, cross browser compatible; but if you're looking for a more up-to-date technique that simplifies the for loop (to omit the if checks), then you can use document.querySelectorAll() as the selector technique:

var boxes = document.querySelectorAll('input[name="demo"][type="checkbox"]:checked'),
    total = 0;

for (var i = 0, len = boxes.length; i < len; i++){
    total += parseInt(boxes[i].value, 10);
}

console.log(total);

JS Fiddle demo.

References:

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

2 Comments

Not bad, would recommend querySelectorAll so that you don't have to use the type check. If he's in college he probably should learn the latest techniques.
Agreed, I was editing that in (after reviewing a suggested edit which slowed me down a moment or two).
0

Use a += and a simple ternary operator:

var total = 0;
total += document.getElementById('cb1').checked ? parseInt(document.getElementById('cb1').value, 10) : 0;
total += document.getElementById('cb2').checked ? parseInt(document.getElementById('cb2').value, 10) : 0;
total += document.getElementById('cb3').checked ? parseInt(document.getElementById('cb3').value, 10) : 0;
alert(total);

I should disclose that is the same as:

var total2 = 0;
if (document.getElementById('cb1').checked) total2 += parseInt(document.getElementById('cb1').value, 10);
if (document.getElementById('cb2').checked) total2 += parseInt(document.getElementById('cb2').value, 10);
if (document.getElementById('cb3').checked) total2 += parseInt(document.getElementById('cb3').value, 10);
alert(total2);

Examples HTML:

<input id="cb1" type="checkbox" value="341" checked />
<input id="cb2" type="checkbox" value="242" />
<input id="cb3" type="checkbox" value="345" checked  />

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.