Connecting Tech Pros Worldwide Forums | Help | Site Map

Hiding multiple Divs cross-browser problem

Newbie
 
Join Date: Nov 2007
Posts: 3
#1: Nov 28 '07
Greetings, and thanks in advance for considering my problem.

I am working in a basic .htm page, in which I want to close all displayed DIVs with one event. In IE, I find that this works:

Expand|Select|Wrap|Line Numbers
  1. function closeAll() {
  2.    var oDiv = new Enumerator(document.body.getElementsByTagName("DIV"));
  3.  
  4.    for(oDiv.moveFirst(); !oDiv.atEnd(); oDiv.moveNext()) {
  5.       if (oDiv.item().style.display == 'inline') {
  6.          oDiv.item().style.display = 'none';
  7.       }
  8.    }
  9. }
  10.  
However, in Firefox it does not. Please forgive me for not starting with Firefox, I work in an all-IE environment and this is my first foray in some time into the real world of cross-browser coding.

So, I tried the following for cross-browser efficacy:

Expand|Select|Wrap|Line Numbers
  1. function closeAll() {
  2.    var oDiv = document.getElementsByTagName("DIV");
  3.  
  4.    for (var i = 0, i < oDiv.length; i++) {
  5.       if (oDiv[i].style.display == 'inline') {
  6.          oDiv[i].style.display = 'none';
  7.       }
  8.    }
  9. }
  10.  
Now IE tells me an object is expected and FF does nothing at all, and I am clearly not clever enough to find my way. Any help you can provide would be very appreciated.

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Nov 29 '07

re: Hiding multiple Divs cross-browser problem


oDiv[i].style.display == 'inline'

Have you set some of your divs' style.display property to 'inline'?
Newbie
 
Join Date: Nov 2007
Posts: 3
#3: Nov 29 '07

re: Hiding multiple Divs cross-browser problem


Quote:

Originally Posted by mrhoo

oDiv[i].style.display == 'inline'

Have you set some of your divs' style.display property to 'inline'?

Yes. There are numerous divs on the page, most of which are hidden when the page loads, but there is always at least one div displaying.

The page is a grid of thumbnail images. Clicking on one of the images "opens" the hidden div associated -- there is a larger image and relevant text and content associated with the thumbnail. Clicking on another image should close the first and open the second, etc. However, I wouldn't know which div had been recently opened, so my thought was to close all divs first and then open the requested div.

Something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <img src="thumbnail_1.jpg" onClick="closeAll(); displayDiv('div_1');">
  3.  
  4.  
But, as mentioned, the closeAll() function is defeating me.

Thanks.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Nov 29 '07

re: Hiding multiple Divs cross-browser problem


How about checking:
Expand|Select|Wrap|Line Numbers
  1. if (oDiv[i].style.display != 'none')
Dasty's Avatar
Expert
 
Join Date: Nov 2007
Location: Slovakia
Posts: 101
#5: Nov 29 '07

re: Hiding multiple Divs cross-browser problem


And correct typo:

Expand|Select|Wrap|Line Numbers
  1. for (var i = 0 ;  i < oDiv.length; i++) 
so "for" wont execute "i < oDiv.length" as init part
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Nov 29 '07

re: Hiding multiple Divs cross-browser problem


Quote:

Originally Posted by Dasty

And correct typo:

Expand|Select|Wrap|Line Numbers
  1. for (var i = 0 ;  i < oDiv.length; i++) 

Ha, good spot!
Newbie
 
Join Date: Nov 2007
Posts: 3
#7: Nov 29 '07

re: Hiding multiple Divs cross-browser problem


Thanks for the feedback. I did try the altered for syntax and fixed the typo. Still get an object expected error on IE and nothin' on FF.

However, in the interim I figured out a fix that does nothing for my knowledge regarding how to close all open divs in a cross-browser way. Since my issue was I didn't really know what div had been opened recently, I decided to approach the problem from that direction.

I created a hidden element on the page and prefilled it with the id for the default open div.

Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" name="displayItem" id="displayItem" value="defaultDiv">
  2.  
Then I wrote this function:

Expand|Select|Wrap|Line Numbers
  1. function swapDivs(sDiv) {
  2.     var oDiv = document.getElementById('displayItem').value
  3.  
  4.     document.getElementById(oDiv).style.display = 'none';
  5.     document.getElementById(sDiv).style.display = 'inline';
  6.     document.getElementById('displayItem').value = sDiv
  7. }
  8.  
Worked pretty nifty, but I discovered that when I refreshed the page the last requested div displayed along with the defaultDiv, so I added this to the onUnload event of the page:

Expand|Select|Wrap|Line Numbers
  1. <body onUnload = "document.getElementById('displayItem').value = 'defaultDiv';">
  2.  
I seem to be set, in terms of being able to move forward. However, I am still curious as to how one might close all open divs in a cross-browser way. If anyone is still willing to guide me, I'd be very pleased to try out your suggestions.

Thanks again.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Nov 30 '07

re: Hiding multiple Divs cross-browser problem


Quote:

Originally Posted by zenbob

Thanks for the feedback. I did try the altered for syntax and fixed the typo. Still get an object expected error on IE and nothin' on FF.

Can you show the divs. Did you try checking that style.display is not equal to "none"? What line do you get the error on in IE?
Reply