35

Below is my javascript code which i used to show a div when clicked on a button.

How can I hide it when clicked again? And then on clicking it, div should be visible again?

<script type="text/javascript">
var _hidediv = null;
function showdiv(id) {
    if(_hidediv)
        _hidediv();
    var div = document.getElementById(id);
    div.style.display = 'block';
    _hidediv = function () { div.style.display = 'none'; };
}
</script>
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
user2827600
  • 383
  • 2
  • 4
  • 5

7 Answers7

71

To switch the display-style between block and none you can do something like this:

function toggleDiv(id) {
    var div = document.getElementById(id);
    div.style.display = div.style.display == "none" ? "block" : "none";
}

working demo: http://jsfiddle.net/BQUyT/2/

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Gigo
  • 3,188
  • 3
  • 29
  • 40
  • 1
    what if display style not added to DIV initially?? – Ahsan Shah Sep 29 '13 at 04:02
  • then div.style.display == "none" will be false and the style will be set to "none" – Gigo Sep 29 '13 at 04:04
  • 1
    for followers, this means "it still works if no div.style.display initially" – rogerdpack Oct 27 '16 at 18:43
  • works for _inline-block_ too `div.style.display = div.style.display == "none" ? "inline-block" : "none";` – pipepiper Nov 01 '16 at 11:30
  • 3
    Note: this will not work on the first click if the div is already hidden when the user loads the page. To combat this, change `div.style.display == "none" ? "block" : "none";` to `div.style.display == "block" ? "none" : "block";` – The Codesee Dec 27 '17 at 18:10
  • what if you want this to be dynamic? I have a function that takes in an id, and toggles the display. This will not work if i use other displays, for example, block, inline-block, flex etc – thatOneGuy Apr 17 '20 at 08:28
34

In case you are interested in a jQuery soluton:

This is the HTML

<a id="button" href="#">Show/Hide</a>
<div id="item">Item</div>

This is the jQuery script

$( "#button" ).click(function() {
    $( "#item" ).toggle();
});

You can see it working here:

http://jsfiddle.net/BQUyT/

If you don't know how to use jQuery, you have to use this line to load the library:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

And then use this line to start:

<script>
$(function() {
    // code to fire once the library finishes downloading.
});
</script>

So for this case the final code would be this:

<script>
$(function() {
    $( "#button" ).click(function() {
        $( "#item" ).toggle();
    });
});
</script>

Let me know if you need anything else

You can read more about jQuery here: http://jquery.com/

Jan
  • 2,462
  • 3
  • 31
  • 56
  • 34
    OP did not tagged jQuery... so why jQuery then ? – Ahsan Shah Sep 29 '13 at 04:00
  • 1
    True, but OP also mentioned he is new at javascript programming, so perhaps he doesn't even know about jQuery and that is why I expanded my answer with jQuery installation as well. – Jan Sep 29 '13 at 04:02
  • `OP` mentioned in his comment `yes, i can use toggle using jquery but i am not allowed to use jquery, Mr. FaceOfJock`. – The Alpha Sep 29 '13 at 04:07
  • @SheikhHeera I started writing this answer before the OP said that. Sorry about that. Anyway, this answer might work for someone else then. – Jan Sep 29 '13 at 04:10
  • just because JQuery wasn't tagged doesn't mean it isn't useful. JQuery *should* be used in this case, it is much easier and makes more sense. This is a great answer and the OP should consider using JQ – samrap Sep 29 '13 at 04:12
  • @Jan, I don't have any problem with your answer, it was just a reminder. – The Alpha Sep 29 '13 at 04:13
  • 1
    @MarkHall, really ? I thought it's a short form `Opposite` till now but learned today, thanks. – The Alpha Sep 29 '13 at 04:16
  • yes sir you are right, i will use jquery now and will also learn about it more, but sir, will this script be able to close div when clicked anywhere on the page, and also when i want to open another div by clicking on another button, will this automatically close – user2827600 Sep 29 '13 at 04:16
  • 2
    @SheikhHeera http://meta.stackexchange.com/questions/40353/stack-exchange-glossary-dictionary-of-commonly-used-terms – Mark Hall Sep 29 '13 at 04:19
  • By default it shows the item content. How can I hide the item content by default. – Rajat Garg Feb 09 '17 at 09:30
  • Perfect. Can you plz tell me if I use your this toggle, how I can make the div invisible in the page load and than togle it if the button is clicked ? – Agil Jan 16 '18 at 13:47
  • @Nikonah just make the item have a display of none on the CSS. – Jan Jan 16 '18 at 16:11
4

jQuery would be the easiest way if you want to use it, but this should work.

function showHide(){
    var e = document.getElementById('e');

    if ( e.style.display !== 'none' ) {
        e.style.display = 'none';
    } else {
        e.style.display = '';
    }
}
Jacques ジャック
  • 3,682
  • 2
  • 20
  • 43
2

You can easily do it with jquery toggle. By this toggle or slideToggle you will get nice animation and effect also.

$(function(){
  $("#details_content").css("display","none");
  $(".myButton").click(function(){
    $("#details_content").slideToggle(1000);
  })  
})
<div class="main">
    <h2 class="myButton" style="cursor:pointer">Click Me</h2>
    <div id="details_content">
        <h1> Lorem Ipsum is simply dummy text</h1> 
        <p>    of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
    </div>
</div>
CodeLover
  • 151
  • 1
  • 7
1

Try following logic:

<script type="text/javascript">
function showHideDiv(id) {
    var e = document.getElementById(id);
    if(e.style.display == null || e.style.display == "none") {
        e.style.display = "block";
    } else {
        e.style.display = "none";
    }
}
</script>
Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
0

with JQuery .toggle() you can accomplish it easily

$( ".target" ).toggle();
Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43
0

Bootstrap 4 provides the Collapse component for that. It's using JavaScript behind the scenes, but you don't have to write any JavaScript yourself.

This feature works for <button> and <a>.

If you use a <button>, you must set the data-toggle and data-target attributes:

<button type="button" data-toggle="collapse" data-target="#collapseExample">
  Toggle
</button>
<div class="collapse" id="collapseExample">
  Lorem ipsum
</div>

If you use a <a>, you must use set href and data-toggle:

<a data-toggle="collapse" href="#collapseExample">
  Toggle
</a>
<div class="collapse" id="collapseExample">
  Lorem ipsum
</div>
Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81