Invisible if css is enabled, don't click
This shouldn't be displaying, if you have css disabled, don't click

Archive for the 'coding' Category

Javascript: determine if element (or any of it’s parents) is hidden. Pt. 2 (a better solution)

Monday, December 4th, 2006

I while ago I posted my code to find out if an element is hidden (with added complexity that if it’s parent is hidden it will also be hidden despite the fact that it’s own display style may not be set.) Originally I came up with the following code:

  1. function displayNoneSet(element){
  2. if(element.parentNode == null){
  3. //we’ve reached the top of the tree #document doesn’t have a style property
  4. //this means that no element is set to hidden on the tree from the original element sent
  5. return false;
  6. }else{
  7. if(element.style.display == "none"){
  8. ///We’ve found a node that is hidden
  9. return true;
  10. }else{
  11. //check the element’s parent, I don’t like recursion, but the only way I see this working
  12. return displayNoneSet(element.parentNode);
  13. }
  14. }

Right away I knew it was a less than perfect example of finding what I needed, I prefer to avoid recursion as much as possible. Eventually I came to the realization that I was overthinking the problem and I came up with a much more elegant solution to the problem. And here it is:

  1. function displayNoneSet(element){
  2. while(element.parentNode){
  3. if(element.style.display=="none"){
  4. //if we have found an element on the DOM tree that is not displayed
  5. return true;
  6. }
  7. element=element.parentNode;
  8. }
  9. //if we’ve got to this point no element was found that had style.display set
  10. return false;
  11. }

This I can live with much easier.

Technorati Tags: , , , ,

Popularity: 48% [?]

Javascript: determine if element (or any of it’s parents) is hidden.

Wednesday, October 25th, 2006

I ran into a problem building a webapp today, I needed a way to determine if a specific element is hidden. Most of the time you can just check it’s style display:

  1. if(document.getElementById("someElement").style.display=="none"){
  2. //…do something
  3. }

… but what happens when it’s parent (or parents parent, or parents parents parent, ad nauseum ad infinitum) is hidden? There is no way (I could tell anyways) to tell if an element is hidden as a result of a parent node somewhere being hidden. Someone had suggested that .offsetParent was set to null in Firefox if the element was hidden, it worked, initially. I found out however this doesn’t work in Internet Explorer and even in Firefox elements such as Select never have an offsetParent. So back to the drawing board, and what I came up with was what I originally had thought would be the solution I’d have to use. The reason I didn’t initially develop it was because it uses recursion, I don’t like recursion, it’s inefficient and looks sloppy but without a better solution I came up with the following:

  1. function displayNoneSet(element){
  2. if(element.parentNode == null){
  3. //we’ve reached the top of the tree #document doesn’t have a style property
  4. //this means that no element is set to hidden on the tree from the original element sent
  5. return false;
  6. }else{
  7. if(element.style.display == "none"){
  8. ///We’ve found a node that is hidden
  9. return true;
  10. }else{
  11. //check the element’s parent, I don’t like recursion, but the only way I see this working
  12. return displayNoneSet(element.parentNode);
  13. }
  14. }
  15. }

And there it is in all it’s simplistic glory, if anyone reading has a better solution, I would like to hear it.

Technorati Tags: , , , ,

Popularity: 57% [?]

Javascript DOM, easy standards compliant programming

Wednesday, August 9th, 2006

One of the things that really bugs me about Javascript is how all the easy stuff is typically the proprietary, non-standard method of programming. I’m big on standards for the web, but I’m a lazy programmer so when it comes to a choice between say, innerHTML and the proper DOM methods there is an inner struggle before my lazy side usually beats down my righteous standards crusader. Now I have much better method that appeals to both sides. The DOM Builder allows me to make standard compliant code while remaining simple to use. Follow the jump for a more detailed description of the problem and solution.

Technorati Tags: , , , ,

(more…)

Popularity: 38% [?]

We did not invent the algorithm. The algorithm killed Jeeves. The algorithm constantly finds Jesus. This is not the algorithm. This is close.