creating a for loop to detect how many selects are on form

N

Nick Calladine

Hi

On my form i have multiple select which all have an id value total1, total2,
total3 etc so i am trying to detect how many there are and then use this to
caculate a total.

Is there a javascript reference to basically go to a form and produce a loop
which will show me how many select drop down boxes there on a form.

Or would i have t use something like

for (var i=0; i < frm.elements.length; ++i) {
form.elements.length
form_field = frm.elements

and then have a nested if to detect if it is select value or has an matching
value

If someone can point me in the right direction i would be gratefull

Thanks
 
N

Nick Calladine

document.getElementsByTagName("select")

thanks commerical but this doesnt give me how many are in the document does
it...

how do u get that... ?

thanks
 
R

RobG

commercial said:
document.getElementsByTagName("select")

That will get every select element in the document, which may not be
appropriate.

[...]
Or would i have t use something like

for (var i=0; i < frm.elements.length; ++i) {
form.elements.length
form_field = frm.elements


var selects = frm.getElementsByTagName('select');

'selects' will be a collection of all the select elements in the
form. To get the value/text of the selected options, you will have
to work out if the select is a multiple or single select and apply
an appropriate algorithm.

[...]
 
N

Nick Calladine

var selects = frm.getElementsByTagName('select');

'selects' will be a collection of all the select elements in the
form. To get the value/text of the selected options, you will have
to work out if the select is a multiple or single select and apply
an appropriate algorithm.

[...]

Rob i am a bit lossed all together now..

All i want to do is have a loop which stops when it basically gone through a
list of select option on a form as each of these have a id tag called name1
, name2, name3
so i would be better off using frm.getElementByID(name) but how do i count
how many there is... and use that value in my loop to tell it stop afters
its the last one.

I dunno if i have confused the question or the answer is confusing me...

Thanks for your time tho..
 
N

Nick Calladine

yep thats a bet clearer ... i think
it was getting mixed up with one my nested loops and i was getting all the
data being returned for the value of each select when i just wanted the
value of the option..

and i got it resolved.. but the answer does give me a bit more light..

thanks for your time again

cheers
Nick
 
S

Stephen Chalmers

Nick Calladine said:
Hi

On my form i have multiple select which all have an id value total1, total2,
total3 etc so i am trying to detect how many there are and then use this to
caculate a total.

I don't think that's quite what you meant. I assume that you want to detect which elements of your form are select
boxes, in order then to read and operate on their values.
To calculate any sort of total, the values first must be converted (and convertible) in some way to numeric values.

The following function returns an array of all the selected values of all selection boxes (of either type) in a named
form.
If none of the selection boxes are set to multiple, the length of the returned array will reflect the number of
selection boxes in the form (for whatever use that may be).

function getAllSelectValues( formName )
{
var allSelected=[];

with( formName )
for( var i=0; i<elements.length; i++ )
if( elements[ i ].type=='select-one' )
allSelected[ allSelected.length ]=elements.options[ elements.selectedIndex ].value;
else
if( elements[ i ].type=='select-multiple' )
for( var j=0; j<elements[ i ].options.length; j++ )
if( elements[ i ].options[ j ].selected )
allSelected[ allSelected.length ]=elements[ i ].options[ j ].value;

return allSelected;
}
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top