Question

I have a calculator that uses javascript. I have some checkboxes that when clicked the javascript will calculate some values and give me a final result. These are in a multipage form (chronoforms). I can click Next Page and those values will pass to the other page. However, if I want to change something, when I click Previous Page, I can see my checkboxes are checked BUT the final result calculated by my javascript calculator is set to the default state (it's not calculating on page load). My only option was to duplicate the calculatetotal function and add a "window.onload =" to that duplicate. It works but I really don't want to leave it like that. Is there something I can do to avoid redundacy in the javascript code? Thanks!

My checkbox has an onclick event that will fire calculateTotal function like so:

onclick="calculateTotal()"

This is the function:

    function calculateTotal(){
 }

When I click "Next Page" and then "Previous Page", My checkbox remains checked, BUT the function calculateTotal does not fire. So I must copy paste the initial function and add:

    window.onload = function calculateTotal(){
 }

The thing is, this function is veeeeeery long and I'd like to avoid redundancy.

Was it helpful?

Solution

My checkbox remains checked, BUT the function calculateTotal does not fire.

Use a call to calculateTotal on the body element:

<body onload = "calculateTotal()">

Make sure the script which includes the function comes before the call.

References

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top