xxxo_tryme
xxxo_tryme

Reputation: 213

I have a problem multiplying the number then getting the sum after. .

    <script language="JavaScript" type="text/javascript"> 
    $(document).ready( function() {


        $(".chkOptions").click(

            function () {
              var multiplier = $('#multiplier').val();

                var y = $('.chkOptions').val() * multiplier;
                $(".chkOptions:checked").each(function () {
                    y += parseInt($(this).val());
                });


                $("#txtSavingsTot").val(y);

            })
            .change();



    });
    </script> 

This code doesn't lead me any where. . . What I wanted to happen is the hidden field multiplier be multiplied to the checkbox

<input type="checkbox" name="cb1" id="cb1" value="60" class="chkOptions" /><input type="checkbox" name="cb2" id="cb2" value="40" class="chkOptions" /><input type="checkbox" name="cb3" id="cb3" value="15" class="chkOptions" /><input type="checkbox" name="cb4" id="cb4" value="20" class="chkOptions" /><input type="hidden" name="multiplier" id="multiplier" value="4" />

What I wanted to happen is beside the checkbox(when checked) the multiplied value & subtotal would be shown, can u help Stackoverflow pros with this problem? Thanks :)

XXXXXXO

Upvotes: 0

Views: 168

Answers (3)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

var multiplier = $('#multiplier').val();

multiplier is a string.

var multiplier = parseInt($('#multiplier').val());

is an integer.

var multiplier = parseFloat($('#multiplier').val());

is a float.

Use whichever one you need.


Edit I'm not quite sure what you're expecting $('.chkOptions').val() to give you.

Upvotes: 6

Calum
Calum

Reputation: 5316

Is this what you're trying to do?

http://jsfiddle.net/afWT2/1/

$(".chkOptions").click(

function() {
    var multiplier = parseInt($('#multiplier').val());

    var y = 0;
    $(".chkOptions:checked").each(function() {
        y = y + parseInt($(this).val());
    });
    $("#txtSavingsSub").val(y);
    y = y * multiplier;


    $("#txtSavingsTot").val(y);

}).change();

Upvotes: 3

Sang Suantak
Sang Suantak

Reputation: 5265

In addition to the answer given by @Tomalak Geret'kal, this line

var y = $('.chkOptions').val() * multiplier;

should be

var y = parseFloat($(this).val()) * multiplier;

Upvotes: 2

Related Questions