Forum Moderators: open

Message Too Old, No Replies

Div Hidden/Visible

         

many tentacles

2:16 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



I am trying to create a button which makes a div appear when clicked and disappear when clicked again.... then appear, then disappear.

I'm nearly there, the div appears, but then rather than just that div disappearing when you click again, everything disappears! I'm sure its simple to work out for people who are confident using Javascript. Not for me though!

Here's the code


<html lang="en">
<head>
<title>Test Page</TITLE>
<style type="text/css">
div.image1 {
position: absolute;
width: 398px;
height: 474px;
top: 88px;
left: 18px;
z-index: 1;
}
div.semitransparent {
position: absolute;
width: 398px;
height: 474px;
top: 88px;
left: 18px;
background-image:url(images/contentfill.gif);
display: none;
z-index: 2;
}
a img { border: none ; }
</style>
<script type="text/javascript">
function hideDivs(){
var arr = document.getElementsByTagName('div')
for(var i=0; i<arr.length;i++){
arr[i].style.display = (arr[i].style.display == 'block')? 'none':'block';
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div><a href="#" onClick="hideDivs();return false;"><img src="images/logo/caricon.gif"></a></div>
<div class="image1"><img src="images/juno/juno1.jpg"></div>
<div class="semitransparent" id="hide1"></div>
</body>
</html>

Hope you can help
Thanks

Bernard Marx

10:34 pm on Mar 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stylesheet CSS properties are separate from inline CSS, from a scripting point of view, and cannot be read directly from an element's style object.

arr[i].style.display

There is no "display" value set for any of the 3 divs (the value is an empty string).
Since [empty string] does not equal "block", all three divs have their style objects' display property set to "block" on the first click. On the second they all get set to "none".

IE has the non-standard, currentStyle property, and there are more advanced ways for dealing with this in standards browsers. For now, I'd just set the display values inline.

viragdom

6:12 am on Mar 22, 2006 (gmt 0)



Try giving the div tag an id, then you can reference it using the getElementById method. Here's the revised code keeping your existing functions.

<html lang="en">
<head>
<title>Test Page</TITLE>
<style type="text/css">
div.image1 {
position: absolute;
width: 398px;
height: 474px;
top: 88px;
left: 18px;
z-index: 1;
display:block;
}
div.semitransparent {
position: absolute;
width: 398px;
height: 474px;
top: 88px;
left: 18px;
background-image:url(images/contentfill.gif);
display: none;
z-index: 2;
}
a img { border: none ; }
</style>
<script type="text/javascript">
function hideDivs(){
var arr = document.getElementsByTagName('div')
for(var i=0; i<arr.length;i++){
arr[i].style.display = (arr[i].style.display == 'block')? 'none':'block';
}
}

function hideDiv(divId){
var arr = document.getElementById(divId)
if ( arr!= null ) {
arr.style.display = (arr.style.display == 'none')? 'block':'none';
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div><a href="#" onClick="hideDiv('imgToHide');return false;"><img src="images/logo/caricon.gif"></a></div>
<div id="imgToHide" class="image1"><img src="images/juno/juno1.jpg"></div>
<div class="semitransparent" id="hide1"></div>
</body>
</html>

Of course the hideDiv function can be modified to close a group of id'd div tags. I've also added display:block; to your image1 css and reversed the style check for block to none. Seems to work fine in IE 6.

Hope it helps,
Dom

Samir_31

10:08 am on Mar 22, 2006 (gmt 0)



just give id to div for eg: <div id="div1">....</div>
and in jscript just write div1.style.visibility="hidden";(for hiding)
div1.style.visibility="visible";(for visible)