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


M

Mechphisto

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
 
Ad

Advertisements

T

Thomas 'PointedEars' Lahn

Mechphisto said:
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
 
G

Gregor Kofler

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
 
G

Gregor Kofler

Am 2010-06-11 15:52, Hans-Georg Michna meinte:
A prepended simple plus sign may also do.

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

Gregor
 
Ad

Advertisements

T

Thomas 'PointedEars' Lahn

Gregor said:
Hans-Georg Michna meinte:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top