Closed Thread Icon

Topic awaiting preservation: Please help - Javascript loop! Pages that link to <a href="https://ozoneasylum.com/backlink?for=23386" title="Pages that link to Topic awaiting preservation: Please help - Javascript loop!" rel="nofollow" >Topic awaiting preservation: Please help - Javascript loop!\

 
Author Thread
htom
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Sep 2004

posted posted 09-22-2004 11:13

Hi!

Could you please help me with Javascript.

I've got 3 <select> fields with names month1, month2, month3.
so it's

<select name="month1">
<option value=1>1
<option value=2>2
<option value=3>3
</select>

<select name="month2">
<option value=1>1
<option value=2>2
<option value=3>3
</select>

<select name="month3">
<option value=1>1
<option value=2>2
<option value=3>3
</select>

I need to run a loop to count the total of months selected for these 3 fields.
So if the customer selects 1 (for month1), 2(for month2), 3 (for month3) then the total=1+2+3=6.

Javascript:

<script language="javascript">
<!--

var i = 0;
var sumtotal=0;

for (i = 0; i < 4; i++)
{sumtotal= document.wizform.monthvalue[i].options.value;}
alert (sumtotal);

// -->
</script>

Not working! document.wizform.monthvalue[i].options.value - Javascript can't understand what the value is. I NEED to use the loop here (i've got plenty of <select> fields... here is only 3 - just for example.... please help. Thanks Helen.

TwoD
Nervous Wreck (II) Inmate

From: Sweden
Insane since: Aug 2004

posted posted 09-22-2004 12:50
code:
<script language="javascript">

var sumtotal=0;
for(j=1;j<3;j++){
var box = document.getElementById("month"+j)
var i = 0;
while((opt = box.options.item(i++))){
if(opt.selected){
sumtotal += parseInt(opt.value);
}
}
}
alert (sumtotal);
</script>



One way of doing it...

/TwoD

htom
Obsessive-Compulsive (I) Inmate

From:
Insane since: Sep 2004

posted posted 09-22-2004 12:59

Sorry I can't see the reply post.

htom
Obsessive-Compulsive (I) Inmate

From:
Insane since: Sep 2004

posted posted 09-22-2004 12:59

hold on. I can now...

TwoD
Nervous Wreck (II) Inmate

From: Sweden
Insane since: Aug 2004

posted posted 09-22-2004 14:20

Oh btw, that's if the user can select more than one option from every selectbox.
In case I misunderstood you the first time, here's a much shorter version if you
allow the user to select only one option from each list.

code:
<script language="javascript">
var sumtotal=0;
for(j=1;j<3;j++){
var box = document.getElementById("month"+j)
sumtotal += parseInt(box.options[box.selectedIndex].value);
}
alert (sumtotal);



Sorry about that

/TwoD

htom
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2004

posted posted 09-22-2004 18:54

Thanks a lot! :-)!!!!!!!!!!!!! working! I may come back because I also need to hide/display layers based on user's selection... but will look into it later.
THANK you!

« BackwardsOnwards »

Show Forum Drop Down Menu