Can't get select values to add as integers instead of strings.

Discussion in 'Javascript' started by Mechphisto, Jun 10, 2010.

  1. Mechphisto

    Mechphisto Guest

    Hi.

    I've a page where I'm trying to add together all the numeric values of
    a set of select boxes, but I can't get the javascript to add the
    numbers as numbers instead of concatenating as strings.

    Here's my script:

    <script type="text/javascript">

    function valSel() {
    var selCount=0;

    var selV1=document.getElementById('list_primaryinfo_1').value;
    var selV2=document.getElementById('list_primaryinfo_2').value;
    var selV3=document.getElementById('list_primaryinfo_3').value;
    var selV4=document.getElementById('list_primaryinfo_4').value;
    var selV5=document.getElementById('list_primaryinfo_5').value;

    selCount = (selCount + selV1);
    selCount = (selCount + selV2);
    selCount = (selCount + selV3);
    selCount = (selCount + selV4);
    selCount = (selCount + selV5);
    selCount = (selCount +
    document.getElementById('list_primaryinfo_6').value);
    selCount = (selCount +
    document.getElementById('list_primaryinfo_7').value);
    selCount = (selCount +
    document.getElementById('list_primaryinfo_8').value);
    selCount = (selCount +
    document.getElementById('list_primaryinfo_9').value);
    selCount = (selCount +
    document.getElementById('list_primaryinfo_10').value);

    if (selCount != 55) {
    alert(selCount);
    return false ;
    }
    }

    </script>

    (You see I tried to process half of them as vars first just to see if
    that'd change anything -- it's didn't.)

    And here's a sample of the the select fields:

    <select name="list_receive_info-su_meetings" size="1"
    id="list_primaryinfo_1">
    <option value="0" selected="selected">--</option>
    <option value="10">10</option>
    <option value="9">9</option>
    <option value="8">8</option>
    <option value="7">7</option>
    <option value="6">6</option>
    <option value="5">5</option>
    <option value="4">4</option>
    <option value="3">3</option>
    <option value="2">2</option>
    <option value="1">1</option>
    </select>
    Service unit meetings<br />
    <select name="list_receive_info-web_site" size="1"
    id="list_primaryinfo_2">
    <option value="0" selected="selected">--</option>
    <option value="10">10</option>
    <option value="9">9</option>
    <option value="8">8</option>
    <option value="7">7</option>
    <option value="6">6</option>
    <option value="5">5</option>
    <option value="4">4</option>
    <option value="3">3</option>
    <option value="2">2</option>
    <option value="1">1</option>
    </select>

    (I also tried it without the quotes surrounding the values, like this:
    <option value=0 selected="selected">--</option>
    <option value=10>10</option>
    <option value=9>9</option>
    but no difference.)

    Any ideas what I can do about this?
    Thanks!
    Liam
    Mechphisto, Jun 10, 2010
    #1
    1. Advertising

  2. Mechphisto wrote:

    > I've a page where I'm trying to add together all the numeric values of
    > a set of select boxes, but I can't get the javascript to add the
    > numbers as numbers instead of concatenating as strings.


    You didn't read the FAQ, did you?


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Jun 10, 2010
    #2
    1. Advertising

  3. Am 2010-06-10 22:58, Mechphisto meinte:
    > Hi.
    >
    > I've a page where I'm trying to add together all the numeric values of
    > a set of select boxes, but I can't get the javascript to add the
    > numbers as numbers instead of concatenating as strings.
    >
    > Here's my script:


    [snipped]

    > Any ideas what I can do about this?


    How about parseInt()? As Thomas said: It's in the FAQ.

    Gregor


    --
    http://www.gregorkofler.com
    Gregor Kofler, Jun 10, 2010
    #3
  4. Am 2010-06-11 15:52, Hans-Georg Michna meinte:
    > On Thu, 10 Jun 2010 23:59:45 +0200, Gregor Kofler wrote:
    >
    >> How about parseInt()? As Thomas said: It's in the FAQ.

    >
    > A prepended simple plus sign may also do.


    Plenty of plus signs then. Whatever the OP prefers - it's all in the FAQ.

    Gregor


    --
    http://www.gregorkofler.com
    Gregor Kofler, Jun 11, 2010
    #4
  5. Gregor Kofler wrote:

    > Hans-Georg Michna meinte:
    >> Gregor Kofler wrote:
    >>> How about parseInt()? As Thomas said: It's in the FAQ.

    >>
    >> A prepended simple plus sign may also do.

    >
    > Plenty of plus signs then. Whatever the OP prefers - it's all in the FAQ.


    Another way is to arrayify the relevant controls, use a mapping function to
    get the Number value and/or Array.prototype.reduce() to compute the sum.
    Requires a recent implementation or a user-defined method, of course, but
    strikes me as being the most elegant solution, almost pythonic (isn't it
    fascinating how programming languages become more similar as each year
    passes?).


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Jun 12, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?J=F8rgen_Cederberg?=

    Problems with conversion of values in strings to integers

    =?ISO-8859-1?Q?J=F8rgen_Cederberg?=, Oct 6, 2003, in forum: Python
    Replies:
    8
    Views:
    310
    =?ISO-8859-1?Q?J=F8rgen_Cederberg?=
    Oct 9, 2003
  2. Replies:
    5
    Views:
    440
    Roy Smith
    May 20, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
  4. palmiere
    Replies:
    1
    Views:
    388
    Erwin Moller
    Feb 9, 2004
  5. Bubba
    Replies:
    4
    Views:
    118
    OmegaJunior
    Mar 31, 2007
Loading...

Share This Page