Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Show Hide Functionality(Javascript)

Posted on 2011-09-14
6
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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 60

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

610 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