473,662 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to hide every <div> ?

I use the following function to hide a <div> named one.

function hideObject() {
if (ns4) {
document.n1.vis ibility = "hide";
}
else if (ie4) {
document.all['n1'].style.visibili ty = "hidden";
}
else if (nn6) {
document.getEle mentById('one') .style.visibili ty = "hidden";
}
}

the code will becomes clumsy if I have many <div> to hide.
Is there good way to hide every <div> ?
Jul 23 '05 #1
19 4454
Hello

One way is to put the IDs of all divs in an array then loop through the
array and hide each element.

Or, I don't know how but know it is possible, loop in the whole page
searching for "div" tags then hide that element.

--
Elias
<be****@yahoo.c om.tw> wrote in message
news:ff******** *************** ***@posting.goo gle.com...
I use the following function to hide a <div> named one.

function hideObject() {
if (ns4) {
document.n1.vis ibility = "hide";
}
else if (ie4) {
document.all['n1'].style.visibili ty = "hidden";
}
else if (nn6) {
document.getEle mentById('one') .style.visibili ty = "hidden";
}
}

the code will becomes clumsy if I have many <div> to hide.
Is there good way to hide every <div> ?

Jul 23 '05 #2
be****@yahoo.co m.tw wrote on 11 aug 2004 in comp.lang.javas cript:
the code will becomes clumsy if I have many <div> to hide.
Is there good way to hide every <div> ?


IE, not tested:

var coll = document.all.ta gs("DIV");
if (coll!=null) {
for (i=0; i<coll.length; i++) {
coll[i].style.visibili ty = "hidden";
}
}

or more static:

<style>
div {visibility:hid den;}
</style>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #3
be****@yahoo.co m.tw wrote:
I use the following function to hide a <div> named one.

function hideObject() {
if (ns4) {
document.n1.vis ibility = "hide";
}
else if (ie4) {
document.all['n1'].style.visibili ty = "hidden";
}
else if (nn6) {
document.getEle mentById('one') .style.visibili ty = "hidden";
}
}

the code will becomes clumsy if I have many <div> to hide.
Is there good way to hide every <div> ?


for ( var i = 0 ; i < document.getEle mentsByTagName{ 'div' ).length ; i++ );
{
document.getEle mentsByTagName{ 'div' )[ i ].style.visibili ty =
'hidden';
}

will work in IE5+, NS6+, Mozilla & Opera and other browsers less than 3
years old :o

Rob
http://www.avagio.co.uk
Jul 23 '05 #4
be****@yahoo.co m.tw wrote:
I use the following function to hide a <div> named one.

function hideObject() {
if (ns4) {
document.n1.vis ibility = "hide";
}
else if (ie4) {
document.all['n1'].style.visibili ty = "hidden";
}
else if (nn6) {
document.getEle mentById('one') .style.visibili ty = "hidden";
}
}

the code will becomes clumsy if I have many <div> to hide.
Is there good way to hide every <div> ?


I've not tried it so can't give you the code, but someone may be able to
fill in the blanks (and its quite an interesting question). There should
be a way to set up a standard style like:-

<style> div {display:block} </style>

Then, in your script, to refer to the document.style. display and set it
from "block" to "none". That should hide them all.
Jul 23 '05 #5
"be****@yahoo.c om.tw" wrote:
I use the following function to hide a <div> named one.

function hideObject() {
if (ns4) {
document.n1.vis ibility = "hide";
}
else if (ie4) {
document.all['n1'].style.visibili ty = "hidden";
}
else if (nn6) {
document.getEle mentById('one') .style.visibili ty = "hidden";
}
}

the code will becomes clumsy if I have many <div> to hide.
Is there good way to hide every <div> ?


Browser detection isn't a very good idea (ns4, ie4, etc variables),
use feature detection. And it's probably a good idea to attempt to
use the most modern mechanism first. Below is a generic function that
will set the visibility of any <div>/<layer> in most browsers to
hidden:

<script type="text/javascript">
function hideObject(id) {
var o;
if (document.getEl ementById) {
o = document.getEle mentById(id).st yle;
} else if (document.layer s) {
o = document.layers ;
} else if (document.all) {
o = document.all[id].style;
}
if (o) {
o.visibility = 'hidden';
}
}

// then

hideObject('you rId1');
hideObject('you rId2');
hideObject('you rId3');

// or

for (var i = 0; i < arrayOfDivIds.l ength; i++) {
hideObject(arra yOfDivIds[i]);
}
</script>

Note that you can *set* "hidden" in Netscape 4, but if queried, NS4
returns "hide" or "show". This script still isn't optimum because it
tests which DOM is supported for each call. The performance
difference probably won't matter in most cases.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #6
Grant Wagner wrote:
Browser detection isn't a very good idea (ns4, ie4, etc variables),
use feature detection. And it's probably a good idea to attempt to
use the most modern mechanism first. Below is a generic function that
will set the visibility of any <div>/<layer> in most browsers to
hidden:

<script type="text/javascript">
function hideObject(id) {
var o;
if (document.getEl ementById) {
o = document.getEle mentById(id).st yle;
} else if (document.layer s) {
o = document.layers ;
document.layers is IIRC an Array object. The object itself
has no visibility property, so I do not understand why you
are including it here. It would be more reasonable to try to
access the id-th layer or not supporting the NN4 DOM at all.
} else if (document.all) {
o = document.all[id].style;
While the IE DOM has an ambiguity that methods of host objects
can be used as collections (and vice-versa), the documentation
at MSDN uses the method syntax and this is why I think one
should stick to that.
}
if (o) {
o.visibility = 'hidden';
}
}
This was my first approach for dhtml.js, too. Yet testing the DOM every
time the method is called is not very efficient. Try this instead:

if (document.getEl ementById)
{
function getElemById(id)
{
return document.getEle mentById(id);
}
}
else if (document.all)
{
function getElemById(id)
{
return document.all(id );
}
}
else if (document.layer s)
{
function getElemById(id)
{
return document.layers[id];
}
}

function hideObject(id)
{
var o = getElemById(id) ;
if (o)
{
(typeof o.style != "undefined" )
? o.style.visibil ity = "hidden";
: o.visibility = "hidden";
}
}
--


Signatures are by convention (RFC) to be delimited by DashDashSpace
(`-- '), so compliant NetNews software (like mine) can handle them
differently (removing them automatically on reply, coloring them to
distinguish them from the rest of the article, hide them if display
is undesired etc.)
<http://insideoe.tomste rdam.com/problems/bugs.htm#sigsep arator>
explains how to achieve that with Outlook Express which will otherwise
truncate trailing spaces on submit no matter how many you type when
composing the mail/article.
PointedEars
Jul 23 '05 #7
Thomas 'PointedEars' Lahn wrote:
Grant Wagner wrote:
o = document.layers ;
document.layers is IIRC an Array object. The object itself
has no visibility property, so I do not understand why you
are including it here. It would be more reasonable to try to
access the id-th layer or not supporting the NN4 DOM at all.


Given that line in the context of the rest of the code, I think it's fairly
obvious my intent was "document.layer s[id]" rather then "document.layer s". A
more appropriate response might have been: "you probably meant
'document.layer s[id]'". A correction of my mistake to the original poster
was necessary, a 4-line explanation on what I did wrong given the context of
the rest of the code was not.

Of course, if your intent was to be intentionally obtuse ("... I do not
understand why you are including it here.") then I guess you succeeded.
} else if (document.all) {
o = document.all[id].style;


While the IE DOM has an ambiguity that methods of host objects
can be used as collections (and vice-versa), the documentation
at MSDN uses the method syntax and this is why I think one
should stick to that.


It's a collection, it gets accessed using the collection syntax. MSDN
documentation indicates that document.forms( ), .links(), .elements(), etc
should also use the method syntax, but I certainly don't use:

<!--[if IE]>
<script type="text/javascript">
var el = document.forms( 'theForm').elem ents('theElemen t');
</script>
<![endif]-->
<![if !IE]>
<script type="text/javascript">
var el = document.forms['theForm'].elements['theElement'];
</script>
<![endif]>

everywhere I access a form element[] or link[].
if (o) {
o.visibility = 'hidden';
}
}


This was my first approach for dhtml.js, too. Yet testing the DOM every
time the method is called is not very efficient. Try this instead:


Which is why I wrote: "This script still isn't optimum because it tests
which DOM is supported for each call. The performance difference probably
won't matter in most cases."

I did not want to confuse the issue by combining more advanced concepts with
the concept of feature detection.
else if (document.layer s)
{
function getElemById(id)
{
return document.layers[id];
}
}
In my environment Netscape 4 is more likely to be in use than Internet
Explorer 4, so I move the document.layers test up simply to document it's
relative importance. Also, I'd probably add the following catch-all else:

else {
function getElemById(id) { return null; }
}
function hideObject(id)
{
var o = getElemById(id) ;
if (o)
{
(typeof o.style != "undefined" )
? o.style.visibil ity = "hidden";
: o.visibility = "hidden";
}
}


Depending on requirements, I might code it as:

function setVisibility(i d, visible) {
var o = getElemById(o);
if (o) {
if (typeof o.style != 'undefined') {
o.style.visibil ity = (visible ? 'visible' : 'hidden');
return (o.style.visibi lity == (visible ? 'visible' : 'hidden'));

} else {
o.visibility = (visible ? 'show' : 'hide');
return (o.visibility == (visible ? 'show' : 'hide'));
}
}
return false;
}

I completely acknowledge the above code is not perfect. It is certainly
possible to get false positives, where the visibility (or other) property is
confirmed as being set on the object but nothing happened in the browser
(Opera 6 and innerHTML are a good example of this). It is possible (but I
feel less likely) to get false negatives. If the visibility property is not
the value you set it to, then either the DOM performed the requested action
and somehow "reset" the value, or the value simply did not get set. Despite
the obvious unreliability of returning a boolean from such a function, I
still think there are circumstances where knowing that setVisibility()
couldn't locate or change the visibility on the specified element can be of
value.
--


Signatures are by convention (RFC) to be delimited by DashDashSpace
(`-- '), so compliant NetNews software (like mine) can handle them
differently (removing them automatically on reply, coloring them to
distinguish them from the rest of the article, hide them if display
is undesired etc.)
<http://insideoe.tomste rdam.com/problems/bugs.htm#sigsep arator>
explains how to achieve that with Outlook Express which will otherwise
truncate trailing spaces on submit no matter how many you type when
composing the mail/article.

PointedEars


Thank you for the link, but I don't use Outlook Express so that article does
not help. I guess you'll just have to mentally filter out the three lines of
my sig.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #8
Thomas 'PointedEars' Lahn wrote:
<snip>
if (document.getEl ementById)
{
function getElemById(id)
{
return document.getEle mentById(id);
}
}
else ...

<snip>

By ECMA specification (section 13) a FunctionDeclara tion is a
SourceElement, and SourceElements may be FunctionDeclara tions or
Statements (section 14). Making it syntactically incorrect for a
FunctionDeclara tion to appear within a Statement (including a block
statement). As a result of this the above must be a FunctionExpress ion
with optional identifier (no other valid interpretation is available
given the surrounding block statement).

However, also by ECMA specification (section 13), the identifier of a
FunctionExpress ion with optional identifier results in a new object
being added to the scope chain of the execution context in which the
expression is evaluated, the identifier is used to create a named
property of that object, a reference to the created function object
assigned to that named property and then the object being removed from
the scope chain. As a result the only code able to utilise the
identifier optionally provided with a FunctionExpress ion to refer to the
corresponding function object is code lexically contained within that
expression.

There is a longstanding bug in the JS implementation used in
Mozilla/gecko where an identifier optionally used with a
FunctionExpress ion leaks into the containing scope, but because that is
contrary to the ECMAScript specification it would not be a good idea to
author code to exploit that fault. The results could not be expected to
be cross-browser.

Richard.
Jul 23 '05 #9
john henry bonham wrote:
<snip>
for (var i = 0 ;i < document.getEle mentsByTagName{ 'div' ).length ;
i++ ); {
document.getEle mentsByTagName{ 'div' )[ i ].style.visibili ty =
'hidden';
}

<snip>

Apart form the absence of verification for the browser's support for the
required features (which can be assumed to be in code previously
executed but not shown), This is a ridiculously inefficient formulation.
You are creating a *new* nodeList each time you read the - length -
property, and another *new* nodeList each time you assign a value to a -
visibility - property. So that is two nodeLists created for each
iteration of the loop (plus one when the length is exceeded).

A nodeList is specified as a "live" collection so we are not even
talking of just the overhead of creating a passive Array each time, but
the creation of quite a capable, complex and active object (then there
is the extra work for the garbage collector in destroying all of these
nodeLists).

It is considerably more efficient to assign a retrieved nodeList to a
local variable once and then iterate through its members by referring to
it through the local variable reference.

Richard.
Jul 23 '05 #10

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

Similar topics

13
3392
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to <foo/>) From XHTML specification:
1
2501
by: Philo | last post by:
How do I select all <div> tags except those which contain a <table> tag somewhere within them? Example XML: <********************** sample input ***********************> <txtSectionBody> <div> <span>
3
84601
by: Paul Thompson | last post by:
When I put a <div ...> inside a <table> specification, functionality is not there. When I put the <table> inside the <div> everything works. Why is that?
8
14461
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a page, there is a huge gap (like 3/8 inch or 25 px) between them. This is driving me bananas. What the hey am I missing? dh ------------------------------------------------ Dan Hansen ------------------------------------------------
2
12182
by: MOHSEN KASHANI | last post by:
Hi, I am trying to hide some form elements in a form by default and show/hide depending on which radio button is clicked. This is what I have but it is not working: <head> <style> ..noshow { display: none; }
3
3806
by: Josef K. | last post by:
Asp.net generates the following html when producing RadioButton lists: <td><input id="RadioButtonList_3" type="radio" name="MyRadioButtonList" value="644" onclick="__doPostBack('SitesRadioButtonList_3','')" language="javascript" />
6
5098
by: cargo303 | last post by:
I have a php page with a drop down list, and the default selected option is "Select a location" (without quotes). Using the drop down initiates a database query. One of (3) things should happen: 1. If an option is selected for which results are available, they should be displayed on the same page beneath the drop down list in a table. 2. If an option is selected for which results are NOT available, a
5
4904
by: widevision7 | last post by:
I have a web site that uses the following JavaScript to hide <div> sections: <script type="text/javascript"> function showMe (it, box) { var vis = (box.checked) ? "block" : "none"; document.getElementById(it).style.display = vis; } </script> I have a checkbox to hide the <div> when checked:
8
10029
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a state where it almost passes the W3C Markup Validation Service. However, the one error, Error Line 166,...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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,...
1
8545
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
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
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...
1
2762
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
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.