How to loop through forms?

B

brett

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
 
R

RobG

brett said:
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.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.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>
 
R

RobG

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--) {
[...]
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top