How to loop through forms?

Discussion in 'Javascript' started by brett, Apr 25, 2005.

  1. brett

    brett Guest

    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
     
    brett, Apr 25, 2005
    #1
    1. Advertising

  2. brett

    RobG Guest

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


    --
    Rob
     
    RobG, Apr 26, 2005
    #2
    1. Advertising

  3. brett

    RobG Guest

    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
     
    RobG, Apr 26, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,420
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,295
    Tim Slattery
    Mar 30, 2005
  3. Roedy Green
    Replies:
    3
    Views:
    459
    Mike Schilling
    Sep 13, 2008
  4. Eric
    Replies:
    2
    Views:
    649
  5. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page