最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Hide and Unhide div with the same button - Stack Overflow

matteradmin2PV0评论

I am using a code that unhides a hidden div.

HTML:

<div id="unhide"  style="display:none;">DUMMY TEXT</div>

<button id="expand" name="expand">Show The Div</button>

JS:

document.getElementById("expand").addEventListener("click", function() 
{
    document.getElementById('unhide').style.display = "block";
});

How can I make the same button hide the div after clicking it again? Is it possible to alter the code I am using now?

I am using a code that unhides a hidden div.

HTML:

<div id="unhide"  style="display:none;">DUMMY TEXT</div>

<button id="expand" name="expand">Show The Div</button>

JS:

document.getElementById("expand").addEventListener("click", function() 
{
    document.getElementById('unhide').style.display = "block";
});

How can I make the same button hide the div after clicking it again? Is it possible to alter the code I am using now?

Share asked Nov 3, 2016 at 12:17 DatacrawlerDatacrawler 2,88610 gold badges52 silver badges107 bronze badges 7
  • 3 like this stackoverflow./questions/4528085/… ? – G.L.P Commented Nov 3, 2016 at 12:19
  • @G.L.P Cheers. I will update the question because the button will have an anchor too. – Datacrawler Commented Nov 3, 2016 at 15:46
  • It is too bad the accepted answer is one involving to use jQuery. Not that I have anything against the library, but since jQuery is not mentioned in your question, a vanilla JS answer would be more useful for other users to understand how to do this without using any toolkit. – dashdashzako Commented Nov 7, 2016 at 10:28
  • @damienc It is the first answer and a simple one (small amount of words). I do not see any vanilla JS answer too. – Datacrawler Commented Nov 7, 2016 at 10:39
  • 1 @ApoloRadomer yes I second that, I just feel like a more plete answer would be good for the munity. I bet many people wonder how to do that kind of UI stuff, and concise answers are good to be copy/pasted, but don't bring as much as I expect in terms of learning. – dashdashzako Commented Nov 7, 2016 at 10:43
 |  Show 2 more ments

9 Answers 9

Reset to default 4

use toggle to simple hide and unhide div

$("#expand").click(function() {
    $("#unhide").toggle();
});

Use toggle for this show and shide, see below code.

$(document).ready(function(){
        $("#expand").click(function(){
            $("#unhide").toggle();
        });
});

By doing some modifications in JavaScript, you can use the same button to hide the div as well as you can change the button text like below.

JS:

document.getElementById("expand").addEventListener("click", function() 
{
    var displayDiv = document.getElementById('unhide');
    var displayValue = (displayDiv.style.display === "block") ? "none" : "block";
    this.innerHTML = (displayValue === "block") ? "Hide The Div" : "Show The Div";
    displayDiv.style.display = displayValue;
});

Link reference: https://jsfiddle/pitchiahn/hctnvsz1/1/

use simple if-else control flow

document.getElementById("expand").addEventListener("click", function() 
{
    var elem = document.getElementById('unhide');

    if(elem.style.display == "none") { elem.style.display = "block"; }
    else { elem.style.display = "none"; }
});

You can use .toggle()

$('#buttonId').on('click', function(e){
    $("#DivId").toggle();
    $(this).toggleClass('class1')
});​

.class1
{
     color: orange;
}​

use toggleClass() to toggle the class for the button

$('#buttonLogin').on('click', function(e){
    $("#login_Box_Div").toggle();
    $(this).toggleClass('class1')
});​

.class1
{
     color: orange;
}​

document.getElementById("expand").addEventListener("click", function() 
{
    if(document.getElementById('unhide').style.display == 'block')
          document.getElementById('unhide').style.display = 'none';
       else
          document.getElementById('unhide').style.display = 'block';
});

you can check the running snippet here

this is pure java script

var button = document.getElementById('button'); // Assumes element with id='button'

button.onclick = function() {
    var div = document.getElementById('newpost');
    if (div.style.display !== 'none') {
        div.style.display = 'none';
    }
    else {
        div.style.display = 'block';
    }
};

This worked very well for me, hope it can help someone else. it opens a hidden div in an absolute position and closes it with the same button or the button in the div. I use it for menu functions.

<div id="myDiv6" style="border:1px solid;background: rgba(255, 255, 255, 
0.9);display: none;position: absolute; top: 229px; left: 25%; z- 
index:999;height: auto;
width: 500px;">
    
<h2 >menu item</h2>
  

what ever you want in the hidden div

<button style="cursor: pointer;border-radius: 12px;background-image: linear- 
gradient(to right, red,yellow);font-size:16px;" 
onclick="changeStyle6()">Close</button>
    
</div>


<br/>
<button style="cursor: pointer;border-radius: 12px;background-image: linear- 
gradient(to right, red,yellow);font-size:16px;width: 125px;" 
onclick="changeStyle6()">button text</button><br/>

<script type="text/javascript">
function changeStyle6(){        
var element = document.getElementById("myDiv6");
if(element.style.display == "none") { element.style.display = "block"; }
else { element.style.display = "none"; }
}    
</script>
Post a comment

comment list (0)

  1. No comments so far