Solved

Show Hide Functionality(Javascript)

Posted on 2011-09-14
6
175 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now