Hide/Show and Toggle DIV usng JavaScript

Shree Limbkar's Avatar author of Hide/Show and Toggle DIV usng JavaScript
This is an article on Hide/Show and Toggle DIV usng JavaScript in JavaScript and AJAX.
Code:
//  The functions have been adapted from various sources
//  and re-written to provide maximum flexibility
//  and compatability with various browsers.

//Global Declarations
var ie = (document.all) ? true : false;

function toggleClass(objClass){
//  This function will toggle obj visibility of an Element
//  based on Element's Class
//  Works with IE and Mozilla based browsers

  if (getElementByClass(objClass).style.display=="none"){
    showClass(objClass)
  }else{
    hideClass(objClass)
  }
}

function hideClass(objClass){
//  This function will hide Elements by object Class
//  Works with IE and Mozilla based browsers

var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    if (elements[i].className==objClass){
      elements[i].style.display="none"
    }
  }
}

function showClass(objClass){
//  This function will show Elements by object Class
//  Works with IE and Mozilla based browsers
var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    if (elements[i].className==objClass){
      elements[i].style.display="block"
    }
  }
}

function toggleID(objID){
//  This function will toggle obj visibility of an Element
//  based on Element's ID
//  Works with IE and Mozilla based browsers
var element = (ie) ? document.all(objID) : document.getElementById(objID);
  if (element.style.display=="none"){
    showID(objID)
  }else{
    hideID(objID)
  }
}

function hideID(objID){
//  This function will hide Elements by object ID
//  Works with IE and Mozilla based browsers
var element = (ie) ? document.all(objID) : document.getElementById(objID);
  element.style.display="none"
}

function showID(objID){
//  This function will show Elements by object ID
//  Works with IE and Mozilla based browsers
var element = (ie) ? document.all(objID) : document.getElementById(objID);
  element.style.display="block"
}

function getElementByClass(objClass){
//  This function is similar to 'getElementByID' since there
//  is no inherent function to get an element by it's class
//  Works with IE and Mozilla based browsers
var elements = (ie) ? document.all : document.getElementsByTagName('*');
  for (i=0; i<elements.length; i++){
    //alert(elements[i].className)
    //alert(objClass)
    if (elements[i].className==objClass){
    return elements[i]
    }
  }
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nominate this article for Article of the month for February 2009
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Vote for this article for Article of the month February 2009
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Using jQuery its a piece of cake.

Code: JavaScript
$('#idOfMyDiv').hide(); // hides the div with id 'idOfMyDiv'
$('.myClassName').hide(); //hides all elements having the classname 'myClassName'
 

Similarly, the show function will unhide the element(s). Enjoy
0
bluantinoo's Avatar, Join Date: Jun 2009
Newbie Member
Hi everybody,
I was looking exactly this thing: how to toggle all elements in a page having a certain class.

I have used this method, but I get this js error:

getElementByClass(objClass) is undefined

does anybody imagine why?
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
use jQuery toggle function http://docs.jquery.com/Effects/toggle
0
gkumar's Avatar
Banned
Hide and Show a Div Using Javascript:-This tutorial will show you how to create a hidden Div and display it with the click of a link. There are a few reasons you may want to hide a Div in a website design. You may want to create a drop down type menu or a box that will show more information when you click a link. Another reason would be for SEO purposes. In theory hiding information in a Div is not against Google’s rules. As long as the user can still see the content when clicking a button you are not displaying different information to the user than you are to the search engine.

First we are going to start with a very basic page layout and create a 2 Divs. One will be shown and one will be hidden. Here is the starting code.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>
</head>
 
<body>
<div id="main">This is not hidden.</div>
 
<div id="hidden">This is hidden.</div>
 
</body>
</html>
 
Ok now I will add some basic style to these boxes.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>
 
<style type="text/css">
<!--
#main{
width:500px;
height: 20px;
background: lightblue;
}
#hidden {
    width:300px;
    height:20px;
    background: lightgrey;
}
-->
</style>
 
</head>
 
<body>
<div id="main">This is not hidden.</div>
 
<div id="hidden">This is hidden.</div>
 
</body>
</html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>
</head>
 
<body>
<div id="main">This is not hidden.</div>
 
<div id="hidden">This is hidden.</div>
 
</body>
</html>
 
Ok now I will add some basic style to these boxes.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hidden Div</title>
 
<style type="text/css">
<!--
#main{
width:500px;
height: 20px;
background: lightblue;
}
#hidden {
    width:300px;
    height:20px;
    background: lightgrey;
}
-->
</style>
 
</head>
 
<body>
<div id="main">This is not hidden.</div>
 
<div id="hidden">This is hidden.</div>
 
</body>
</html>
Seven ways to toggle an element with JavaScript:-There are litterally an unlimitted number of ways to toggle an element’s display with JavaScript. Some, more useful than others. Dating back to the late nineties, toggling is perhaps the oldest trick in the book within JavaScript development. However, to this day, it still proves itself useful as hiding/showing elements can improve user interaction (when done tastefully).

Anyway, here are seven ways toward achieving just that.
The bottom line
First off, this is perhaps the simplest and shortest way to toggle.
toggling the short way

Code:
// showing
document.getElementById('element').style.display = '';
// hiding
document.getElementById('element').style.display = 'none';
So let’s make this into a function that’ll get the job done real quick like:
show? or hide?

Code:
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}
Well that was easy. But we can also go ternary style.
toggling ternary style
Code:
function toggle(obj) {
	var el = document.getElementById(obj);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
}
Cool. Hey, ever heard of the dollar function? If that’s sittin’ around somewhere, let’s use it!
toggling with the ternary dollar
Code:
// our dollar function
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
// and now our improved toggler!
function toggle(obj) {
	var el = $(obj);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
}
Cool. Now that we’ve got this thing nice and compact… can’t we do anything else to make it cool? Why, of course. Let’s say, instead of limitting it to just one object, let’s say “unlimitted.”
toggling multiple objects
Code:
function toggle() {
	for ( var i=0; i < arguments.length; i++ ) {
		$(arguments[i]).style.display = ($(arguments[i]).style.display != 'none' ? 'none' : '' );
	}
}
Sweet. Now that looks nice and sexy (quite personally, I think it’s the sexi’est’). But let’s see if we can extract any of these into “showing” versus “hiding” using an object literal.
separating showing and hiding
Code:
var toggle = {
	show : function(obj) {
		document.getElementById(obj).style.display = '';
	},
	hide : function(obj) {
		document.getElementById(obj).style.display = 'none';
	}
};
Alright alright alright, I can dig that. But what happened to using our dollar buddy? Surely we can get that back in the mix.
separating showing and hiding
Code:
var toggle = {
	show : function(obj) {
		$(obj).style.display = '';
	},
	hide : function(obj) {
		$(obj).style.display = 'none';
	}
};
Ok, why not even give it the flexibility of passing in as many arguments as we want to either? Ok, no problem.
separating showing and hiding
Code:
var toggle = {
	show : function() {
		for ( i=0; i < arguments.length; i++ ) {
			$(arguments[i]).style.display = '';
		}
	},
	hide : function() {
		for ( i=0; i < arguments.length; i++ ) {
			$(arguments[i]).style.display = 'none';
		}
	}
};
0
naimish's Avatar
Banned
Are this all old articles ? I mean before May-June ?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
gkumar, Please do not copy everything from everywhere and paste here

http://www.dustindiaz.com/seven-togglers/
0
David Michael's Avatar, Join Date: Jul 2009
Go4Expert Member
awesome!this is the best article for me!It would be easy if they show it in xhtml and in a css sheet.