Loop Through Visible Elements

Discussion in 'Javascript' started by Dave R, Aug 10, 2004.

  1. Dave R

    Dave R Guest

    Hey everybody!

    I'm in dire need of some help.
    I'm trying to loop through all the elements in a form, and determine
    whether or not the field is visible. Kinda like this:

    var TForm = document.form1;

    for (i=0;i<TForm.length;i++)
    {
    if (TForm.elements.style.visibility=="visible") { // this line is
    wrong
    ... do stuff here ...
    }
    }

    This isn't working at all though...
    Any suggestions or help would be greatly appreciated!.
    Thanks in advance.
    Dave
     
    Dave R, Aug 10, 2004
    #1
    1. Advertising

  2. On 9 Aug 2004 16:38:42 -0700, Dave R <> wrote:

    > I'm trying to loop through all the elements in a form, and determine
    > whether or not the field is visible. Kinda like this:
    >
    > var TForm = document.form1;
    >
    > for (i=0;i<TForm.length;i++)
    > {
    > if (TForm.elements.style.visibility=="visible") { // this line is
    > wrong
    > ... do stuff here ...
    > }
    > }
    >
    > This isn't working at all though...


    Working with inline styles can be quite awkward. This is because when a
    value matches what has been imposed by a stylesheet, the respective style
    property returns an empty string rather than the actual value. This does
    have its advantages though; older browsers don't support values such as
    "display: table-row". So, if you hide a table row then want to show it
    again, some values have to be given "display: block" whilst others need
    "display: table-row". Setting an empty string for the display property
    does both, effectively.

    In your case, visible elements have a visibility property that matches an
    empty string, and 'hidden' when they are hidden. Try:

    var form = document.forms['formName'];

    for(var i = 0, n = form.length; i < n; ++i) {
    var elem = form.elements;

    if(elem.style && 'hidden' != elem.style.visibility) {
    // Element is visible
    }
    }

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail
     
    Michael Winter, Aug 10, 2004
    #2
    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. spamfurnace
    Replies:
    1
    Views:
    2,638
    Scott M.
    May 24, 2004
  2. tshad

    button visible/not visible

    tshad, Oct 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,451
    Brian Hoops
    Oct 28, 2004
  3. tshad

    Sections visible and not visible

    tshad, Jan 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,065
    tshad
    Jan 31, 2005
  4. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    5,976
    Patrick Olurotimi Ige
    Jun 15, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page