473,698 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.le ngth; ++i) {
form.elements.l ength
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
7 2120
document.getEle mentsByTagName( "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******** ***********@new sfe2-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.le ngth; ++i) {
form.elements.l ength
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
> document.getEle mentsByTagName( "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
commercial wrote:
document.getEle mentsByTagName( "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.le ngth; ++i) {
form.elements .length
form_field = frm.elements[i]


var selects = frm.getElements ByTagName('sele ct');

'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
>
var selects = frm.getElements ByTagName('sele ct');

'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.getElementB yID(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
Nick Calladine wrote:
var selects = frm.getElements ByTagName('sele ct');

'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.getElementB yID(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.g etElementsByTag Name("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.getElements ByTagName('sele ct');

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.getEle mentById("IdOfe Select")
}

Now it's not nessary to use document.getEle mentById() 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
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
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******** ***********@new sfe2-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 getAllSelectVal ues( formName )
{
var allSelected=[];

with( formName )
for( var i=0; i<elements.leng th; i++ )
if( elements[ i ].type=='select-one' )
allSelected[ allSelected.len gth ]=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.len gth ]=elements[ i ].options[ j ].value;

return allSelected;
}

--
Stephen Chalmers

547265617375726 520627572696564 206174204F2E532 E207265663A2054 51323437393134
Jul 23 '05 #8

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

Similar topics

6
2732
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything else is so well done that the way you create forms seems to be too cumbersome, in particular making it so that a pull down menu selects a value that you assign it. In any case, does anyone know of any php based (or other readily accepted web...
6
2945
by: NotGiven | last post by:
I want to learn moer of what I saw in a recent example. They create a page that created new fields/element. It's not like they were hidden and they displayed them, they were not there, then the script created them. It used things like parentnode, insertBefore, appendChildNode,... Any ideas/direction is appreciated.
5
3462
by: Nick Calladine | last post by:
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
2
2224
by: Andrea | last post by:
I'm having some difficulty creating a report in Access and I need some suggestions. My company issues "Return Authorizations" when customers need to return products. A customer calls in and we provide the number. The customer then returns items for a refund or for an exchange. Here is the basic set up of the tables in question. -ReturnAuthorizationTable- ReturnNumber_PK CustomerID ReturnType
8
10752
by: Edwinah63 | last post by:
Hi Everyone, in vb6 i was able to execute the following code and it would close the children is the reverse order they were opened eg the last child opened was the first child to close. in mdiparent i had this code i = Forms.Count - 1 Do Until i = 0
12
17052
by: Patrick Dugan | last post by:
I have an vb.net application that is a module that uses a "application.run" in the sub main to start. There is no form involved (just a system tray icon) How can you detect when the application is being closed? It is easy enough if the user selects "exit" from this tray icon but how can you detect if Windows is closing the program down? Normally I would simply do something in the Form.Closing event but without a form how can you...
4
1881
by: DeanL | last post by:
Hi Guys, I need some help creating a query that is going to take between 1 and 10 parameters. The parameters are entered on a form into text boxes that may have data or be empty. Is there a way to create a single query that will take parameters if they are present in the text boxes and not take parameters if the text box is empty? The ten fields will need to be searched using "Like" so that the user can search on part of a text...
52
6325
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
2
5064
by: ckerns | last post by:
I have a page with a bunch of drop down boxes. They are named: MonEquipAMWk1 MonEquipPMWk1 thru FriEquipAMWk1 FriEquipPMWk1
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.