View Full Version : Convert from toPrecision to toExponential with Check Box

03-28-2012, 10:40 PM
I have a function which populates a form with input boxes. The default is set to toPrecision(X)

However, I would like to give the user the option to switch to toExponential(X) via check box (or some other method). It would either have to update the current values to exponential or just clear the list and then they have to start over. Similarly, the uncheck should bring it back to Precision.

Also, is there a way to have the option carried through to multiple pages? Or will this just be a temporary toggle and it would get lost when going to the next page?

P.S. I am very new to JS so try to be nice :)

03-28-2012, 11:00 PM
If your checkbox is defined using:

<input type="checkbox" id="exp"> <label for="exp">use toExponential()</label>

and if the field that is to have the method applied to it is amt and the field it is to be put in is called result

then the JavaScript would be:

result = (document.getElementById('exp').checked) ? ant.toExponential(X) : amt.toPrecision(X);

No JavaScript carries through between pages unless you store values in a cookie and read them back on the following page.

Old Pedant
03-28-2012, 11:07 PM
Surely it would be better to have it recreate the "list" rather than having the user start over?

But you don't mention what the "list" is.

Is it a bunch of calculations? Based on user input? Or what?

I think you need to show the <form> and its contents to get a full answer.

03-28-2012, 11:59 PM
Thanks for the responses :) I am still trying to digest it, but here are few things:

- I currently have two sets of pages. One has precision, the other exponential. This does not change the list in real time, but does keep the "current" setting active. It is a bigger maintenance issue as well (2 sets of pages).

- If I need a cookie to store the setting, I may not want to go that route. I want to use the HTML/JS in phonegap and covert it to an app. But I don't know if phonegap will support a cookie (but perhaps it will). I will have to see into that before I consider that approach.

Old Pedant
03-29-2012, 01:01 AM
You could also send the setting along as part of the URL.

That is, if the user sets it on page1.html and you want to go to page2..html and carry it along, you could do something like

location.href = "page2.html?setting=" + setting;

or, if you go from one page to the next via <form>s,

<form action="page2.html" method="get" >
<input type="hidden" name="setting" value="toPrecison" />
and just use JS to change the value of the hidden field.

Many ways to skin the cat.

03-29-2012, 02:46 AM
I have to think about that for a bit, but it makes sense. Thanks for the tips.