function showhide(id, id2){
if (document.getElementById){
obj = document.getElementById(id);
obj2 = document.getElementById(id2);
if (obj.style.display == "none"){
obj.style.display = "inline";
obj2.innerHTML = "Hide";
} else {
obj.style.display = "none";
obj2.innerHTML = "Show";
}
}
}
Here is the html. I float the link to the right side of the page. The link is outside the div so that is doesnot dissappear.
<hr>
<span style="display:block;float:right">
... Block Title ...
<a id="flag1" href="#" onclick="showhide('div1','flag1');">Hide</a>
</span>
<div id="div1" style="display:inline">
... stuff I want to hide
</div>
<hr>
Any improvements or comments are certainly welcome.






6 Comments
// pass the DIV id from onclick()
function fold(theid) {
var theid=document.getElementById(theid);
theid.style.display=(theid.style.display=="block")? "none" : "block";
}
NOTE: needs bobsawyerdotcom's "fold()" function to work
/**
* Show hide many elements by ids
* ie: show_hide( 'tag1_id, tag2_id, tag3_id' );
*/
function show_hide(ids)
{
var list_of_ids = new Array;
// check for , in id
if( ids.indexOf(',') )
{
list_of_ids = ids.split(',');
}
// do show/hide based on css display = none
// it will assume that the element is showing if no css "display" is found
if( list_of_ids.length > 0 )
{
for(i=0; i < list_of_ids.length; i++)
{
// show hide ele
fold(list_of_ids[i]);
}
}
else // one id
{
// show hide ele
fold(ids);
}
}
theid.style.display=(theid.style.display=="block")? "none" : "block";
to:
theid.style.display=(theid.style.display=="none") ? "block" : "none";
This is that when you have no display set, the element will hide first time and not be set to "block" (so you don't have to click on it twice).
So heres the full function:
function fold(theid) {
var theid=document.getElementById(theid);
theid.style.display=(theid.style.display=="none") ? "block" : "none";
}
Hey you know AdGuy always gets the last word! ;)