473,654 Members | 3,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining current state of style.display and toggling it

Greetings.

I am trying to write a function which toggles the display of a certain
class of <div> elements in an HTML page. The CSS file initially sets some
classes to "display: none", and others to "display: block". The problem I
am encountering is that when the JavaScript interpreter starts up
initially, all style.displays are null. Thus the following code is not
effective:

function toggleAll(itemn ame) {
tmp = document.getEle mentsByTagName( 'div');
for (i = 0; i < tmp.length; i++) {
if (tmp[i].className == itemname) {
if (tmp[i].style.display == 'none')
tmp[i].style.display = 'block';
else
if (tmp[i].style.display == 'block')
tmp[i].style.display = 'none';
}
}
}

In the CSS file:
..foo { display: block; }
..bar { display: none; }

In the HTML file:
<a href="#" onclick="toggle All('foo')">Sho w/hide foos</a>
<a href="#" onclick="toggle All('bar')">Sho w/hide bars</a>
I can change the second innermost if statement to the following:

if (tmp[i].style.display == 'block' || tmp[i].style.display == '')

but this doesn't work either, since the bar class is initially hidden; the
user then needs to click the "Show/hide bars" link twice before the bars
toggle.

So I guess what I need is to query the current state of
tmp[i].style.display to see if it's "none" or "block". How do I do this?

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Jul 23 '05 #1
1 3558
Tristan Miller wrote:
The CSS file initially sets some classes to "display: none", and
others to "display: block".
If this document is to be placed on the Web, you should avoid doing
that. If a user agent doesn't provide the necessary support to show
that data, the user will be unable to see the content. Instead, you
should hide the content via scripting.
The problem I am encountering is that when the JavaScript
interpreter starts up initially, all style.displays are null.
In this case, yes, but not as a rule. The style object reflects inline
styling. That is, style data applied either through the style object
itself, or via the HTML style attribute.

[snip]
So I guess what I need is to query the current state of
tmp[i].style.display to see if it's "none" or "block". How do I do
this?


You have two options:

1) Follow the advice I gave previously. If you hide the content via
scripting, you can be sure that 'none' means hidden and '' (an empty
string) means visible.
2) Obtain the computed style for an element[1]. This will reflect the
value of a CSS property no matter how it was applied.

var getComputedValu e = (function(v) {
if(v && v.getComputedSt yle) {
return function(e, p, pE) {
return v.getComputedSt yle(e, pE || null)[p];
};
} else {
v = null;
return function(e, p) {
if(e && e.currentStyle) {return e.currentStyle[p];}
};
}
})(document.def aultView);

if('block' == getComputedValu e(myDiv, 'display')) {
/* ... */
}

Hope that helps,
Mike
[1] It should be noted that whilst IE does provide a proprietary
method of obtaining this information, it is deficient; values aren't
calculated and are returned literally.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17300
by: Csaba2000 | last post by:
I have set onmousedown to change the cursor, but this setting is ignored (IE 5.5; NN 6.1 on Win 2K Pro) until the mouse is either moved or the mouse button is released. On Opera 7.01, the setting seems to be ignored completely, even when I try with window.setTimeout. So my two questions are: (1) Most important: Is there anything I can do so that I don't have to wait for the next mouse event before the cursor gets repainted. (2) Why...
16
4429
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a href="lemurs.html">Lemurs</a></li> <li><a href="chameleons.html">Chameleons</a></li> </ul> <ul>
13
1508
by: Razzbar | last post by:
Is there any to tell via javascript the "state" of a link? I mean, I'd like to be able to tell if a link has been followed or not. (hehe, I can hear shrill crys of "privacy!" in the distance...)
13
5534
by: Gunnar | last post by:
Hello, I am running into problems with a simple function which should change the style.display properties from 'block' to 'none'. I am able to change them from 'none' to 'block' as expected. Any suggestions are appreciated. The Javascript: function toggleTable(id){ var allTables = document.getElementsByTagName('table');
4
2150
by: Lachlan Hunt | last post by:
Hi, I'm looking for an interoperable method of determining the current background colour of an element that is set using an external stylesheet. I've tried: elmt.style.backgroundColor; but that only works if the colour has been set using the style attribute or previously set using script. I've also tried:
15
12479
by: Markus Ernst | last post by:
Hi When toggling an element on and off by setting its display property via DOM access, display:none is valid for all kinds of elements, but I can't find anything about a generic value for toggling on again. Furthermore, UAs do not recognize the same values. To make a table row visible, IE wants display:block while FF wants display:table-row. Am I missing something? I am looking for something like display:normal
10
1625
by: Peter Olcott | last post by:
Someone told me that determining the exact location and current state of any JavaScript controls is pretty easy. Does anyone know exactly how this is done?
11
2869
by: Nospam | last post by:
I don't know what it is I am doing wrong, I am trying to get the menus to either expand or contract based on their previous states, i.e if already expanded if clicked again contract, and if contracted, expand, so far it doesn't work for the about and services link(but works for the expand all and contract all links), any help would be greatly appreciated: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
1
4278
by: ll | last post by:
I'm currently working on a form which consists of a show and hide javascript. The toggle works fine, although when I click on submit, I would like the page to reload with the toggle (show/hide) div in the same state it was before being submitted. E.G. If the div was visible, I'd like for it to be visible upon return. Here's the script I'm currently using - thanks for any help, Louis --------------------------------------------
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.