0

I have a problem, and I can't seem to work out how to solve it. I have two checkboxes, both of them have a value of 5. I just need to add the value of 5 to a variable when a checkbox is checked, and deduct 5, when it is unchecked. I've been using jQuery, but so far I've only managed to add 5 and it adds 10, when both of them are checked. It doesn't deduct anything as of now. Since I have a some radio buttons which I properly check, the code I have is this.

$(":checked").each(function(){
    if ($(this).attr("type") == "checkbox"){
        pricediff += 5;
    }
}

as the function each is used, it adds 10 when both of them are checked. This, of course, is not wanted. And it still has to deduct 5 when the checkbox is unchecked. I have no idea how to do that - I'm a bit of a noob to jQuery and JavaScript. Can anyone assist me? Thanks

P.S. this is not the exact code but just the jist of it.

1
  • To raise the chance people will help you, it's advised to accept some answers of your previous questions first. Commented Nov 23, 2010 at 6:48

3 Answers 3

2

@rahul has a good answer, but this implies all checkboxes must have a value of 5.

Try this demo: http://jsfiddle.net/2WJZ4/

HTML:

<input type="checkbox" class="test" value="5" />
<input type="checkbox" class="test" value="7" />
<input type="checkbox" class="test" value="2" />
<input type="checkbox" class="test" value="1" /><br />
<span id="result">0</span>

Script:

$(function(){
    $("input.test").change(function(){
        var $this = $(this);
        var value = $this.val();
        var checked = $this.is(":checked");
        if (checked) value = -value;
        var $r = $('#result');
        $r.text( (+$r.text()) - value)
    });
});

PS. @rahul Awesome fiddle page!!!

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

Comments

1

Sample HTML

<input type="checkbox" class="test" />
<input type="checkbox" class="test" />
<input type="checkbox" class="test" />
<input type="checkbox" class="test" /><br />
<span id="result">0</span>

Script

$(function(){
    $("input:checkbox.test").click(function(){
        var checked = $("input:checkbox.test:checked").length;
        var checkedValue = checked * 5;

        $("#result").text(checkedValue)
    });
});

See a working demo

2 Comments

Thank you. That's the most reasonable answer. I know it works here, but in the context of my code, it doesn't work like it should, since there's a lot more values and they all have to be added on. And I can't just add checkedValue all the time. I have to know when to deduct. Thanks for your help though, you've lead me on to the right track.
indeed - see my example below.
1

try something like this
var placediff;
function calc()
{
var n = $("input:checked").length;
pricediff = n*5;
}
$(":checkbox").click(calc);

1 Comment

This is right. I checked in the context of my code and it didn't work. It does answer my exact question though, and it does work like I asked it to. Thank you.

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.