0

I have Jquery/Javascript function which calculates certain field values. One of the field returns a value in

function calcTotals() {
    marginObj.value = ((totalObj.value * 1) - (total_inr_valueObj.value * 1)).toFixed(3);
}

I have another function with a checkbox

function recalcTotal() {
    var total12 = 0;
    $('input:checked').each(function () {
        total12 += $(this).marginObj.value;
    });

    $('#total12').val(total12);
}
$("input[type='checkbox']").on("click", function () {

    recalcTotal();
});

I need to pass the values from the marginObj.value to the next function that is total12 += $(this).marginObj.value; What iam trying to achieve is iam taking the values from marginObj.value and there is a checkbox, if i check the checbox it totals all the checked values and show it in grandTotal field.

  <input name="grandTotal" id="total12" readonly/>

Please check the complete script. FIDDLE

So what i want is there are some values in Margin with a checkbox. When i check any checkbox, its should total the selected checkboxes and show in the Grand Total field.

8
  • What is the html for checkbox? Commented Nov 19, 2015 at 5:29
  • 1
    Can you make a working code snippet in your post, create a fiddle or a codepen so we have a working example to work with? Commented Nov 19, 2015 at 5:29
  • <input type='checkbox' name='gt[]' id='gt[]'><input size='8' type='text' id='margin_for[]' name='margin_for[]' readonly> The values of margin_for is passing to marginObj.value.. Commented Nov 19, 2015 at 5:30
  • @SkullDev. The code is too long. so am thinking how to post it. Commented Nov 19, 2015 at 5:31
  • I am guessing the issue is $(this).marginObj.value should be $(this).next("input").val(). Edited Commented Nov 19, 2015 at 5:52

3 Answers 3

1

I am guessing the issue is getting the marginObj which is an input element after check box.

$(function () {
   recalcTotal();

   $("input[type='checkbox'").on("click", function () {
       recalcTotal();
    });

   function recalcTotal() {
        var total = 0.0;

        $("input:checked").each(function () {
            total += $(this).next("input").val() * 1.0;
        });

        $("#total").val(total.toFixed(3));
    }
});

<input type="text" id="total" readonly/> <br />
<input type="checkbox" name="values"/><input type="text" readonly value="1"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="2"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="3"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="4"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="5"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="6"/> <br />
Sign up to request clarification or add additional context in comments.

5 Comments

This code should work. do not use .marginObj.value just use the code as is.
Its working fine. Just one more query. How will i make the totals to 3 decimals.
and one more query. If i dont select any checkbox, by default it should show the existing margin totals.
Its ok, i guess you are busy. Really really appreciate your help. Its really a great help to novices like me to get help and motivated from members like you. Really thankyou for your time...:)
@SanjuMenon Sorry I was away (different time zone) :). to make 3 decimal change * 1 to * 1.0 and to call recalcTotal() on page load. see edited version.
0

Not 100% what you are asking, but if you are simply trying to pass a value from one function to another, there are multiple approaches, but the simplest is to pass arguments to the function.

//this function supports 1 argument
function calcTotals( amount ) {
    //add 1 and return
    return amount + 1;
}

//calling this argument
var amount = 5;
var newAmount = calcTotals( amount );

3 Comments

Might be worth putting a return example from one function to another as well since OP seems fairly new.
i will try to shortened the code and put in fidle so that you can have a look.
I have updated my question. I have added my existing script in fiddle
0

assuing marginObj is globally accessible.

function recalcTotal() {
var total12 = 0;
$('input:checked').each(function () {
    total12 += $(this).marginObj.value;
});

$('#total12').val(total12);
}
 $("input[type='checkbox']").on("click", function () {
calcTotals(); -- will populate marginObj.value
recalcTotal();
});

2 Comments

i will try to shortened the code and put in fidle so that you can have a look.
I have updated my question. I have added my existing script in fiddle

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.