Solved

Show Hide Functionality(Javascript)

Posted on 2011-09-14
6
178 Views
Last Modified: 2012-05-12
Hi - I need someway of showing and hiding elements on screen based on a button click etc - is there a way in JavaScript to do this ?

Thanks
0
Comment
Question by:abuyusuf35
  • 4
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36537686
Test page : http://jsfiddle.net/r3SQd/

<input id="button1" type="button" value="show/hide" />
<div id="elementInside">
    HERE ALL YOUR ELEMENT TO SHOW/HIDE
</div>

Open in new window


<script>
window.onload = function() {
    document.getElementById("button1").onclick = function() {
        var els = document.getElementById("elementInside");
        if( els.style.display == "none") {
            els.style.display = "block";
        }
        else {
            els.style.display = "none";
        }
    }
}
</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36537692
another one : http://jsfiddle.net/r3SQd/1/
<input id="button1" type="button" value="hide" />
<div id="elementInside">
    HERE ALL YOUR ELEMENT TO SHOW/HIDE
</div>

Open in new window

<script>
window.onload = function() {
    document.getElementById("button1").onclick = function() {
        var els = document.getElementById("elementInside");
        if( els.style.display == "none") {
            els.style.display = "block";
            this.value = "hide";
        }
        else {
            els.style.display = "none";
            this.value = "show";
        }
    }
}
</script>

Open in new window

0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36537706
yes, you can put an onclick function on the button or any other element that you want to use and then the easiest way is for the javascript to manipulate the css.
So then you do something like this
document.getElementById('idofelementtohid').style.display="none";
You can do this with any css style property.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36537707
Another one : http://jsfiddle.net/r3SQd/3/
<a href="javascript:void(0)" id="button1" >hide</a>
<div id="elementInside">
    HERE ALL YOUR ELEMENT TO SHOW/HIDE
</div>

Open in new window

<script>
window.onload = function() {
    document.getElementById("button1").onclick = function() {
        var els = document.getElementById("elementInside");
        if( els.style.display == "none") {
            els.style.display = "block";
            this.innerHTML = "hide";
        }
        else {
            els.style.display = "none";
            this.innerHTML = "show";
        }
    }
}
</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36537735
with jQuery : http://jsfiddle.net/r3SQd/8/
<a href="javascript:void(0)" id="button1" >hide</a>
<div id="elementInside">
    HERE ALL YOUR ELEMENT TO SHOW/HIDE
</div>

Open in new window

<script src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script>
$(document).ready(function() {
    $("#button1").click(function() {
        $("#elementInside").toggle();
        $(this).html( $("#elementInside").is(":visible")?"hide":"show" );
    })
})
</script>

Open in new window


0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36537773
here is another simple sample using jQuery
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" ></script>

<script>
function btnClick(){
  $("#divTest").toggle();
}
</script>

<div id=divTest style="width:200;border:1px solid gray">HainKurt</div>
<button onClick="btnClick()">Toggle div</button>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide Table in merge 3 32
function parameter and using that as array 15 23
Html value of radio 14 30
Only allow one check box 17 27
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question