473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Learning : Loop to list all dropdown box values on a form

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


Jul 23 '05 #1
5 3451
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
Jul 23 '05 #2
------------ 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>


Hi Andy

Well i am sorry i been playing with this code all day long trying to get it
to work.. these are my first few days in javascript but i can get rid of the
error
here my code

it basically reports

Line:28
Char:1
Code:0
Error:Object expected

which points to the select line <select id="itemname0" name="processors"
size="1" onchange="total()">

can you help is it me ... aarrghh

Nick

<script>

function caculateTotal()
{
for( var i = 0; i <= 5; i++ )
{
var eSelect = document.getElementById("itemname" + i)
if( eSelect != null )
{
alert( eSelect.value );
}
}
}

</script>
</head>

<body>

<select id="itemname0" name="processors" size="1" onchange="total()">
<option value="No Processor {0.00}">Select Processor</option>
<option value="2800 {466.73}">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> Processor Options <br>

</body>
Jul 23 '05 #3
Nick Calladine wrote:
------------ 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>


Hi Andy

Well i am sorry i been playing with this code all day long trying to get it
to work.. these are my first few days in javascript but i can get rid of the
error
here my code

it basically reports

Line:28
Char:1
Code:0
Error:Object expected

which points to the select line <select id="itemname0" name="processors"
size="1" onchange="total()">

can you help is it me ... aarrghh

Nick

<script>

function caculateTotal()
{
for( var i = 0; i <= 5; i++ )
{
var eSelect = document.getElementById("itemname" + i)
if( eSelect != null )
{
alert( eSelect.value );
}
}
}

</script>
</head>

<body>

<select id="itemname0" name="processors" size="1" onchange="total()">
<option value="No Processor {0.00}">Select Processor</option>
<option value="2800 {466.73}">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> Processor Options <br>

</body>


Hi Nick

Read your line of HTML....

<select id="itemname0" name="processors" size="1" onchange="total()">

Note the : onchange="total()"

This is looking for a funciton called "total()"

Whereas the funciton you have pasted above is "caculateTotal()".

This would deffinaly cause an "Object Expected".

HTH

Andy
Jul 23 '05 #4
it the words of homer simpson

DOH

thanks andy... (works a teat now....)

Jul 23 '05 #5


Nick Calladine <n i c k c a l l a d i n e @ n t lw orl d . c o m (remove all spaces)> wrote in message
news:bb******************@newsfe1-win.ntli.net...
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") {

}
}
}


It's not clear in what form you want the data, or what if any operation you want to perform within the loop that
accesses it.
To take the subject line literally, the following minimum change to your presented code, alerts a list of specified
selected elements.
document.getElementById is not always available, so should not be used for accessing form elements.

function caculatetotal(frm)
{
var order = "";

for (var i=0 ; i < frm.elements.length; i++)
if (frm.elements[i].type=='select-one' && frm.elements[i].name.substring(0,8) == "itemname")
order += frm.elements[i].options[frm.elements[i].selectedIndex].value+"\n";

alert( order );

return order;
}

caculatetotal( document.forms['myform'] );

--
Stephen Chalmers
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes...
1
by: Les Juby | last post by:
Simple problem that's stumping me at present.! We have created a data capture form in Access 97 which uses a dropdown list which has been populated from a table. The dropdown list displays the...
2
by: Tom van Stiphout | last post by:
Hi All, This one has me stumped. I hope someone else has seen this behavior and knows what's going on. I'm using Access 2003 ADP on Windows XP, database in 2002/2003 format, with SQL Server 2000....
0
by: Manish | last post by:
Hi All, I'm having this problem I hope someone can help provide a solution for it :) I've this dropdown list box in a usercontrol which I'm populating from the database (it's viewstate property...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
2
by: gleadams | last post by:
I have databound a Windows form ComboBox control to a DataSource and DataMember and it is properly showing the data as I navigate through the records. My question concerns the choices in the...
4
by: Paul | last post by:
Hi all, I have a page that has a form on it which has a dropdown list on it. It connect to an sql database and populate the list. What I would like to do is make the list editable so that if the...
3
by: John | last post by:
I have two dropdown lists that I have bound to a datatable and set the DataTextField and DataValueField for. Both lists show the values I expect from the database. However, when I need to access...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.