SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist webfreebies's Avatar
    Join Date
    Apr 2003
    Location
    NYC
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Silly problem re: strings and numbers

    I've got a silly, little problem that I can't seem to figure out. I am creating a quiz that uses radio buttons. Whenever a button is selected, it passes its numeric value to a function, which then gets added along with all the other numeric values that are passed.

    The form looks like this:

    1. This is the first question
    <blockquote><form name="quiz">
    <input type="radio" name="q1" value=1 CHECKED> Option one
    <br><input type="radio" name="q1" value=2 > Option Two
    <br><input type="radio" name="q1" value=3 > Option three
    <br><input type="radio" name="q1" value=4 > Option Four</blockquote>

    2. This is the second question
    <blockquote><input type="radio" name="q2" value=1 CHECKED> Option one
    <br><input type="radio" name="q2" value=2 > Option Two
    <br><input type="radio" name="q2" value=3 > Option three
    <br><input type="radio" name="q2" value=4 > Option Four</blockquote>
    <br><input type="button" value="Do It!" onClick="additup();">
    <br></form></blockquote>
    The function looks like this:

    <script language="javascript">
    <!--
    function additup()
    {
    for (var i=0; i<4; i++)
    {
    if (document.quiz.q1[i].checked)
    {
    var sum1=document.quiz.q1[i].value;
    }
    if (document.quiz.q2[i].checked)
    {
    var sum2=document.quiz.q2[i].value;
    }
    }
    var totals= sum1+sum2
    document.write (totals)
    }
    //-->
    </script>
    The problem is that the *sums* get added as strings and not actual numbers. So instead of 1+1 equalling 2, I will get "11."

    Secondary problem: Doesn't seem to work in Netscape 4.7. Is that stemming from a glitch in that version of Netscape, or have I screwed up somewhere? Thanks!
    Retro-Ads.net- vintage ads from the 20s-80s.
    NYC-Pics.com- free photos of NYC

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    radio.value is a String, the + operator both adds numbers and combines Strings (hence, no error). You'll need to use parseInt() to get the results you're looking for.

  3. #3
    SitePoint Evangelist webfreebies's Avatar
    Join Date
    Apr 2003
    Location
    NYC
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much! *thumbs up*

    (Back to the salt mines for me.)
    Retro-Ads.net- vintage ads from the 20s-80s.
    NYC-Pics.com- free photos of NYC

  4. #4
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to have this woking fine in Netscape try using window.document.forms.formname.control.value

    I had similar problems with Netscape, used as above and problems solved. Thanks to a tip right here on this form, though i was unable to find that post here right now.


Bookmarks

Posting Permissions

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