Solved

Show Hide Functionality(Javascript)

Posted on 2011-09-14
6
176 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

919 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

17 Experts available now in Live!

Get 1:1 Help Now