Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can't get select values to add as integers instead of strings.

Reply
Thread Tools

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

 
 
Mechphisto
Guest
Posts: n/a
 
      06-10-2010
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').valu e);
selCount = (selCount +
document.getElementById('list_primaryinfo_7').valu e);
selCount = (selCount +
document.getElementById('list_primaryinfo_8').valu e);
selCount = (selCount +
document.getElementById('list_primaryinfo_9').valu e);
selCount = (selCount +
document.getElementById('list_primaryinfo_10').val ue);

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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-10-2010
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$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Gregor Kofler
Guest
Posts: n/a
 
      06-10-2010
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
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      06-11-2010
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-12-2010
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$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output of select values are numerals instead of state and county Bubba Javascript 4 03-31-2007 04:32 PM
to add 2 very long integers vicky_in C++ 6 04-19-2006 02:27 PM
Help, need to add two integers to a queue and check if they are in there later Mark Java 1 04-16-2005 10:19 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM
Problems with conversion of values in strings to integers =?ISO-8859-1?Q?J=F8rgen_Cederberg?= Python 8 10-09-2003 11:05 AM



Advertisments