Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Convert from toPrecision to toExponential with Check Box

    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

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,632
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If your checkbox is defined using:

    Code:
    <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:

    Code:
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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
    Code:
    location.href = "page2.html?setting=" + setting;
    or, if you go from one page to the next via <form>s,
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ronjon65 (03-29-2012)

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    32
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I have to think about that for a bit, but it makes sense. Thanks for the tips.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •