0

Im building a simple form where im trying to pass values from check boxes...

<div class="checkboxclass">
<input name="form[paperdesign][]" value="150" id="paperdesign0" type="checkbox">
<label for="paperdesign0">text 1</label>
<input name="form[paperdesign][]" value="100" id="paperdesign1" type="checkbox">
<label for="paperdesign1">text 2</label>
<input name="form[paperdesign][]" value="50" id="paperdesign2" type="checkbox">
<label for="paperdesign2">text 3</label>
<input name="form[paperdesign][]" value="50" id="paperdesign3" type="checkbox">
<label for="paperdesign3">text 4</label>
</div>

...using this function:

function calculate(){
    var sela=document.querySelectorAll("div.checkboxclass input");
    resultsel.value=0;
    resultsel.value=parseInt(resultsel.value);
    for(i=0;i<sela.length;i++)
        resultsel.value=parseInt(resultsel.value)+parseInt(sela[i].value);
}

And it works OK apart from the fact that it's passing this all the values at this same time.

Could comeone please help me out on this one?

many Thanks in advance

Dom

1
  • What do you mean by "it's passing this all the values at this same time"? Commented Nov 9, 2010 at 17:03

2 Answers 2

1

If I understand the question, you mean that you only want to include the values for the checked items. In this case, you should be able to use the :checked selector:

var sela=document.querySelectorAll("div.checkboxclass input:checked");
Sign up to request clarification or add additional context in comments.

1 Comment

Small things and made me so happy! Many Thanks!
1

I think you want it to only calculate selected checkboxes.

You have to check for checkbox selection:

function calculate(){
    var sela=document.querySelectorAll("div.checkboxclass input");
    var total = 0;
    for(i=0;i<sela.length;i++) {
        if(sela[i].checked)
            total += parseInt(sela[i].value);
    }
    resultsel.value = total;
}

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.