By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,440 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,440 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i]

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
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
document.getElementsByTagName("select")

"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:5q*******************@newsfe2-win.ntli.net...
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[i]

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


Jul 23 '05 #2

P: n/a
> document.getElementsByTagName("select")

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

how do u get that... ?

thanks
Jul 23 '05 #3

P: n/a
commercial wrote:
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[i]


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
Jul 23 '05 #4

P: n/a
>
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


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..
Jul 23 '05 #5

P: n/a
Nick Calladine wrote:
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

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..

Nick

If you don't know how many selects there are on a page using the
ParentElement.getElementsByTagName("select") (ParentElement can be any
element be it a body, form, div etc.) will return a collection of
elements the are below the ParentElement (Don't have to be directly
below) that are selects.

The number of selects that are below the ParentElement can be obtain
from the .length property of the collection that is returned.

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

As Rob has said this will return a collection af ALL selects below the
form element.

So using the following you can cycle through the collection

for( i = 0; i < selects.length; i++ )
{
var eSelect = selects[i];
// at this point eSelect is a select element
// which is the same as document.getElementById("IdOfeSelect")
}

Now it's not nessary to use document.getElementById() again once you
have the collection of elements. As you can see above using selects[#]
you can reference the element.

HTH

Andy
Jul 23 '05 #6

P: n/a
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
Jul 23 '05 #7

P: n/a
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:5q*******************@newsfe2-win.ntli.net...
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[i].options[ elements[i].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;
}

--
Stephen Chalmers

547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.