...

View Full Version : How to determine wether a Node is rendered visible or not?



greenone
03-14-2006, 12:51 PM
Hi there,

i came up zero when trying to find a direct method/value in the javascript object of the Node to determine wether an object is shown or not. So i wrote the following function. it checks relative to a parent (which could by document.body or a form or something) if somewhere on the way to that parental fixpoint a display='none' is found.
If theres a better/faster or even direct way to do this please let me know, otherwise enjoy this function.

P.S. i did not optimize it for IE since it doesn't support the prototype anyway



Node.prototype.visibleByInherit=function(s) {
var p=this;
var v=true;
do {
if(p.style!=null)
if(p.style.display!=null)
if(p.style.display.toLowerCase()=='none')
v=false;
p=p.parentNode;
} while(p!=null && p.parentNode!=null && p!=s.parentNode && v);
return v;
}

Basscyst
03-14-2006, 06:42 PM
Well, you may have run into a problem because javascript only detects inline styles. So if you declared the display in the style sheet you won't be able to detect it's current display property until you have set in the javascript.

Basscyst

Kravvitz
03-14-2006, 07:18 PM
Also you have not accounted for the visibility property, elements being absolutely positioned off the left side of the page, or elements being obscured by another element. Perhaps you are happy with just checking the display property.

This may help: http://www.quirksmode.org/dom/getstyles.html

brothercake
03-15-2006, 01:05 AM
Well, you may have run into a problem because javascript only detects inline styles. So if you declared the display in the style sheet you won't be able to detect it's current display property until you have set in the javascript.

Basscyst
Ever heard of "getComputedStyle" (or IE's equivalent, which is "currentStyle")? Google for them - they allow the reading of computed styles quite apart from (or rather, including but not exclusively) styles declared in inline attributes.

But I would echo what Kravvitz said - there are many ways an element could be hidden, not just display - there's visibility and offleft-positioning, and also clip, overflow, opacity ... quite a lot of things that could result in an element not being visible, which taken together makes this a somewhat non-trivial task.

Can I ask why you need to know? What's the bottom line here?

Basscyst
03-15-2006, 01:25 AM
Ever heard of "getComputedStyle" (or IE's equivalent, which is "currentStyle")? Google for them - they allow the reading of computed styles quite apart from (or rather, including but not exclusively) styles declared in inline attributes.


Nope sure hadn't. Have now. I thank you. :)

Basscyst

Kravvitz
03-15-2006, 05:29 AM
Ever heard of "getComputedStyle" (or IE's equivalent, which is "currentStyle")? Google for them - they allow the reading of computed styles quite apart from (or rather, including but not exclusively) styles declared in inline attributes.
*points at link in previous reply* :p

brothercake
03-15-2006, 07:43 AM
*points at link in previous reply* :p
Touche :D

greenone
03-15-2006, 03:31 PM
thank you thats just what i needed to know, somehow i doubted that theres no elegant way to get the rendered style

i only need to know if an element has display none or anything else so its just fine, thanks alot

Buckley
04-02-2007, 09:23 AM
fortunately there is a better way :

f = document.getElementById('idOfYourForm');

alert(f.elements['idOfYourInputElement'] == null);

if this returns false than its not rendered

Hope this helps, buck

Buckley
04-02-2007, 09:56 AM
hmz Forget my previous post. It does not work.

Buckley
04-02-2007, 10:13 AM
I also did something similar as your code but as a function :

function elementVisible(jsid) {

jselement = document.getElementById(jsid);

do {

if (jselement.style.display.toUpperCase() == 'NONE') return false;

jselement=jselement.parentNode;

}while (jselement.tagName.toUpperCase() != 'BODY');

return true;

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum