473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(itemname) {
tmp = document.getElementsByTagName('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="toggleAll('foo')">Show/hide foos</a>
<a href="#" onclick="toggleAll('bar')">Show/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 3537
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 getComputedValue = (function(v) {
if(v && v.getComputedStyle) {
return function(e, p, pE) {
return v.getComputedStyle(e, pE || null)[p];
};
} else {
v = null;
return function(e, p) {
if(e && e.currentStyle) {return e.currentStyle[p];}
};
}
})(document.defaultView);

if('block' == getComputedValue(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
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...
16
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...
13
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
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. ...
4
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...
15
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...
10
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
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...
1
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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,...

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.