Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to loop through forms?

Reply
Thread Tools

How to loop through forms?

 
 
brett
Guest
Posts: n/a
 
      04-25-2005
Using DOM in IE, how can I loop through FORMs and access FORM elements
in a specific form? For example, www.hotmail.com has about 13 forms.
I believe the one displayed is dependent on the URL. If I want to
access the submit button of the visible form, which is usually the last
one, how is that done?

Currently, I look for type=submit using DOM but it doesn't find
anything and only loops through the first FORM elements.

Sorry if this is the wrong group to ask DOM questions.

Thanks,
Brett

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-26-2005
brett wrote:
> Using DOM in IE, how can I loop through FORMs and access FORM elements
> in a specific form? For example, www.hotmail.com has about 13 forms.
> I believe the one displayed is dependent on the URL. If I want to
> access the submit button of the visible form, which is usually the last
> one, how is that done?
>
> Currently, I look for type=submit using DOM but it doesn't find
> anything and only loops through the first FORM elements.



The script below will loop through all the forms and toss an alert
for each submit button chanced upon, however given the situation you
describe, your bigger task is working out which form is displayed,
and hence which is the submit button you are after.

The form maybe hidden as a result of its own display property being
set to 'none' or visibility to 'hidden', but it may also be hidden by
modifying the above attributes of a containing element (div, td,
etc.).

So firstly you must find the form that is visible, then find its
submit button.

>
> Sorry if this is the wrong group to ask DOM questions.


DOM questions related to JavaScript are fine.


<html>
<head><title>Forms & buttons</title>
<script type="text/javascript">
function showSubmits(){

// Get a collection of all the forms
var allForms = document.forms;
var els, j, i = allForms.length;

// The following loop goes thru all the forms, the last one is:
// var lastForm = allForms[j-1];
while (i--) {

// Get a collection of all the elements of this form
els = allForms[i].elements;
j = els.length;

// Depending on some parameter, decide whether to do the
// following loop that finds the submit button within the form

// Look for a submit button
while (j--) {
if ( 'submit' == els[j].type ) {

// Do something with it...
alert('Found a submit ' + els[j].nodeName
+ ' for ' + allForms[i].name);
}
}
}
}
</script>
</head>
<body>
<form name="aForm">
<input type="text" name="aText">
<input type="submit">
</form>
<form name="bForm">
<input type="text" name="bText">
<input type="submit">
</form>
<form name="cForm">
<input type="text" name="cText">
<button>Plain button</button>
</form>
<input type="button" onclick="showSubmits()"
value="Show submit buttons">
</body>
</html>


--
Rob
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-26-2005
RobG wrote:
[...]

Sheeesh...

> function showSubmits(){
>
> // Get a collection of all the forms
> var allForms = document.forms;
> var els, j, i = allForms.length;
>
> // The following loop goes thru all the forms, the last one is:
> // var lastForm = allForms[j-1];


The last one is of course:

// var lastForm = allForms[i-1];

> while (i--) {

[...]

--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments