Click here to Skip to main content
5,787,682 members and growing! (20,565 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

Hide and Show Any Element

By senthil karuppaiah

Hide and show any element based on some event
Javascript, HTML, Windows, Visual Studio, Dev

Posted: 19 Aug 2006
Updated: 21 Sep 2008
Views: 34,432
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.78 Rating: 3.29 out of 5
1 vote, 14.3%
1
0 votes, 0.0%
2
1 vote, 14.3%
3
3 votes, 42.9%
4
2 votes, 28.6%
5

Introduction

If you want to hide and show an element or DIV or SPAN that contains relevant information for a particular item, e.g., when the user clicks on a particular link, you may want to show some details.

It is easy to do this using JavaScript.

If you want to display personal information for each person in the list (it may be a <A> or RADIO or SELECT element), when you generate the HTML, render the personal information section within a SPAN or DIV tag which is hidden by default. If you use SPAN, then make sure whether you want to display the following text on the same line or on the next line.  

<DIV ID="PersonalInfo1" style="display:none">
<table>
<tr><td>Street</td><td>123 sr</td></tr>
<tr><td>City</td><td>LA</td></tr>
<tr><td>State</td><td>CA</td></tr>
</table>
</DIV>

Now you need to connect this DIV with the actual person. It may be <A> tag or some button or radio button or select tag.

<A href="#" onclick="show('PersonalInfo1')">Senthil</A> 

or:

<input type="radio" value="senthil" onclick="show('PersonalInfo1')">senthil

or:

<SELECT name="select1" onchange="show(this.value)">
<option value=""></option>
<option value="PersonalInfo1">senthil</option>
</select>

or:

<input type="button" onclick="show('PersonalInfo1')" value="personalInfo">

By default, SPAN is an inline tag meaning it will just follow the flow and shift the position based on left and top properties. In that case, you need to use 'inline' to make it display or set display = ''. If you use DIV, you should use display = "block";

  function show(ele) {
         var srcElement = document.getElementById(ele);
         if(srcElement != null) {
	   if(srcElement.style.display == "block") {
     		  srcElement.style.display= 'none';
   	    }
            else {
                   srcElement.style.display='block';
            }
            return false;
       }
  }

When you click on the hyperlink, the browser will show the table. If you click again, the browser will hide it.

Another way is that you can have a common detail area. Then when the user clicks a link or button or selects an item from the drop down, you can display details in  the target.

<DIV ID= "PersonalInfo"></DIV>

<A href="#" onclick="show('PersonalInfo1','
<table><tr><td>Street</td><td>123 sr</td></tr></table>
')">Senthil</A>

<A href="#" onclick="show('PersonalInfo','
<table><tr><td>Street</td><td>123 sr</td></tr></table>
')">Raj</A>
  function show(ele,content) {
      var srcElement = document.getElementById(ele);
      if(srcElement != null) {
       srcElement.innerHTML=content;
      }
  }

The above code will replace the inner HTML of the DIV with the new TABLE or:

<DIV ID= "PersonalInfo"></DIV>

<A href="#" onclick="show('PersonalInfo','PersonalInfo1')">senthil</A>

<DIV id="PersonalInfo1" style="display:none"><table>
<tr><td>Street</td><td>123 sr</td></tr>
<tr><td>City</td><td>LA</td></tr>
<tr><td>State</td><td>CA</td></tr>
</table>
')">
  function show(targetElement,contentElement) {
      var tarElement = document.getElementById(targetElement);
      var srcElement = document.getElementById(contentElement);
      
      if(tarElement != null && srcElement  != null) {
       tarElement.innerHTML=srcElement.innerHTML;
      }
  }

In the above code, we define different DIVs for each personal information and connect them with the link by sending the correct DIV ID for the second argument.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

senthil karuppaiah


Fourteen years of progressive experience in Software Product Development, tactical planning, project and client management, demonstrated success in leadership, critical thinking, problem solving and analysis. Diverse knowledge and experience with multiple development methodologies, reengineering, software engineering, and integration of heterogeneous systems.
Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralCODE ERROR in the above script! READ THISmemberusbserial16:20 18 Sep '08  
GeneralRe: CODE ERROR in the above script! READ THISmembersenthil karuppaiah6:35 21 Sep '08  
GeneralRe: CODE ERROR in the above script! READ THISmemberusbserial14:15 21 Sep '08  
GeneralNicely Explainedmembernaman2:06 28 May '08  
GeneralPlease help me..memberkalyandama21:38 13 Mar '08  
GeneralThanksmembersamad_sami23:21 13 Mar '07  
GeneralA few comments (or constructive criticism)memberBjoern Graf14:44 19 Aug '06  
GeneralRe: A few comments (or constructive criticism)membersenthil karuppaiah16:07 19 Aug '06  
GeneralRe: A few comments (or constructive criticism)memberBjoern Graf16:36 19 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Sep 2008
Editor: Deeksha Shenoy
Copyright 2006 by senthil karuppaiah
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project