Skip to content Skip to sidebar Skip to footer

Passing Jquery Value To Html And Then Tally The Totals With Onclick

I am trying to pass on JQuery values to hidden textboxes (to send as a form later) as well as divs t hat displays on the front end. I also want to tally these items as the value is

Solution 1:

Update #total_div text in addNumber function as,

functionaddNumbers()
{
var val1 = parseInt(document.getElementById("my_value_1").value);
var val2 = parseInt(document.getElementById("my_value_2").value);
var ansD = document.getElementById("total");
ansD.value = val1 + val2;
$('#total_div').text(ansD.value);
}

Demo

Solution 2:

click(addNumbers('total')); first calls addNumbers with an unused parameter 'total' then gets the return value of addNumbers (null or undefined) and sets that as the click() handler for the next click.

I think you probably meant

$('#my_div').click(addNumbers);

that means, "run the addNumbers function, defined below, next time I click my_div".

or just

addNumbers();

that means, "run the addNumbers function now" (at the first click)

Note though that when you click and call addNumbers, one of the numbers may not yet be copied, so you would be adding 100+"" or ""+200 so you really have to think about what you want to do.

Post a Comment for "Passing Jquery Value To Html And Then Tally The Totals With Onclick"