Loop Through Visible Elements

D

Dave R

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
 
M

Michael Winter

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
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top