Nick Calladine wrote:
Learning : Loop to list all dropdown box values on a form
Can some one point me in the right direction :
I have a form which I want to loop through
I basically want to get all the selected values of the option tag
see example below
<select name=itemname3 size="1"
onchange="calculatetotal(document.myform)">
<option value="2800 {466.73}" selected>2800 AMD 64 </option>
<option value="3000 {478.18}">3000 AMD 64</option>
<option value="3200 {505.20}">3200 AMD 64</option>
<option value="3400 {515.14}">3400 AMD 64</option>
<option value="3700 {617.14}">3700 AMD 64</option>
</select>
so i want to be able to read the option value ("2800 {466.73)") of the
string if it has been selected or is the default value.
The selectname of the dropdown boxes are all labeled itemname and increment
from 3 to variable x
heres my code so far
function caculatetotal(frm)
var order = 0
var counter = 0
for (counter=0 ; counter <frm.element.length; ++x) {
form_field=frm.elements[counter]
form_fieldname = form_field.name
if (form_fieldname.substring(0,8) == "itemname") {
}
}
}
so i basically need to start the loop to detect itemname3 onwards.. but not
too sure how to do this...
anyhelp would be appreciated.
thanks
Hello Nick
If i understand you correctly you have X number of selects on a page.
And you want obtain their values.
You have a number of ways to achive this.
1. This is my prefered way, would be to give the selects an ID's each so
that you could use document.getElementById("mySelectId"). This returns
the element that has that ID (only one element can have a given ID).
2. Another is document.getElementsByName("mySelectName"), this returns a
collection of elements that match the given name ( any number of
elements can have the same name ).
3. Assuming that you only have selects on the page that you are
interested in would be to use
document.body.getElementsByTagName("select") this will return a
collection of all the select elements that are below body.
This is how i would do it based on your current nameing convention but
using ID's instead.
------------ SAMPLE CODE -------------
<select id="itemname0">.. options ..</select>
<select id="itemname1">.. options ..</select>
<select id="itemname2">.. options ..</select>
<select id="itemname3">.. options ..</select>
<select id="itemname4">.. options ..</select>
<select id="itemname5">.. options ..</select>
<script>
function caculateTotal()
{
for( var i = 0; i <= 5; i++ )
{
var eSelect = document.getElementById("itemname" + i)
if( eSelect != null )
{
alert( eSelect.value );
}
}
}
</srcipt>
-------------- END CODE --------------
HTH
Andy