Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Hide/Show and Toggle DIV usng JavaScript (http://www.go4expert.com/articles/hide-toggle-div-usng-javascript-t16094/)

Shree Limbkar 6Feb2009 11:14

Hide/Show and Toggle DIV usng JavaScript
 
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]
    }
  }
}


shabbir 4Mar2009 09:56

Re: Hide/Show and Toggle DIV usng JavaScript
 
Nominate this article for Article of the month for February 2009

shabbir 17Mar2009 12:17

Re: Hide/Show and Toggle DIV usng JavaScript
 
Vote for this article for Article of the month February 2009

pradeep 24Mar2009 16:26

Re: Hide/Show and Toggle DIV usng JavaScript
 
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

bluantinoo 5Jun2009 02:30

Re: Hide/Show and Toggle DIV usng JavaScript
 
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?

pradeep 5Jun2009 10:29

Re: Hide/Show and Toggle DIV usng JavaScript
 
use jQuery toggle function http://docs.jquery.com/Effects/toggle

gkumar 3Jul2009 15:18

Re: Hide/Show and Toggle DIV usng JavaScript
 
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';
                }
        }
};


naimish 3Jul2009 15:23

Re: Hide/Show and Toggle DIV usng JavaScript
 
Are this all old articles ? I mean before May-June ?

shabbir 3Jul2009 15:37

Re: Hide/Show and Toggle DIV usng JavaScript
 
gkumar, Please do not copy everything from everywhere and paste here

http://www.dustindiaz.com/seven-togglers/

David Michael 6Jul2009 19:17

Re: Hide/Show and Toggle DIV usng JavaScript
 
awesome!this is the best article for me!It would be easy if they show it in xhtml and in a css sheet.

kien_vn 31Aug2010 12:10

Re: Hide/Show and Toggle DIV usng JavaScript
 
I usually use this function, it work fine

Code:

var ie = (document.all) ? true : false;
function toggleObj(obj) {
 
    try{
        var el;
        try{
       
            el = (ie) ? document.all(obj) : document.getElementById(obj);
        }
      catch(err)
      {
            el= document.getElementById(sm);
      }

        if ( el.style.display != 'none' ) {
            el.style.display = 'none';
           
        }
        else {
            el.style.display = 'block';
           
        }
    }catch(err)
    {
   
    }
}



All times are GMT +5.5. The time now is 03:09.