From: Mechphisto on
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
From: Thomas 'PointedEars' Lahn on
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$8300dec7(a)news.demon.co.uk>
From: Gregor Kofler on
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
From: Hans-Georg Michna on
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.

Hans-Georg
From: Gregor Kofler on
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