36

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>
8
  • Give me one moment, I'll get you a demo.
    – Jacques
    Sep 29, 2013 at 3:48
  • Using jquery you can use toggle , very simple
    – Charaf JRA
    Sep 29, 2013 at 3:51
  • thanks jack for considering this question, take your time and please give me an answer... thanks once again. Sep 29, 2013 at 3:52
  • Do you have a button for each div ?
    – The Alpha
    Sep 29, 2013 at 3:53
  • 1
    @user2827600, Your HTML will help to understand the way you want it, otherwise I can think of this.
    – The Alpha
    Sep 29, 2013 at 4:03

7 Answers 7

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/

6
  • 1
    what if display style not added to DIV initially??
    – Ahsan Shah
    Sep 29, 2013 at 4:02
  • then div.style.display == "none" will be false and the style will be set to "none"
    – Gigo
    Sep 29, 2013 at 4:04
  • 1
    for followers, this means "it still works if no div.style.display initially"
    – rogerdpack
    Oct 27, 2016 at 18:43
  • works for inline-block too div.style.display = div.style.display == "none" ? "inline-block" : "none";
    – pipepiper
    Nov 1, 2016 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"; Dec 27, 2017 at 18:10
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/

12
  • 35
    OP did not tagged jQuery... so why jQuery then ?
    – Ahsan Shah
    Sep 29, 2013 at 4: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, 2013 at 4: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, 2013 at 4:07
  • 1
    @MarkHall, really ? I thought it's a short form Opposite till now but learned today, thanks.
    – The Alpha
    Sep 29, 2013 at 4:16
  • 2
    @SheikhHeera meta.stackexchange.com/questions/40353/…
    – Mark Hall
    Sep 29, 2013 at 4:19
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 = '';
    }
}
5
  • what if display style not added to DIV initially??
    – Ahsan Shah
    Sep 29, 2013 at 4:06
  • The display should automatically be block, especially if you use a css reset, which I would highly recommend.
    – Jacques
    Sep 29, 2013 at 4:08
  • nope buddy, i have tried your code but it is not working as expected
    – Ahsan Shah
    Sep 29, 2013 at 4:10
  • You are correct, I'm sure I was using this code somewhere though. Give me a min unless you already have your answer.
    – Jacques
    Sep 29, 2013 at 4:17
  • Ok, well a pure js, no jQuery working solution is now in my answer. Link: lucienconsulting.com/dev
    – Jacques
    Sep 29, 2013 at 4:22
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>

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>
2
  • actually i was not clear from question... but from comments now.
    – Ahsan Shah
    Sep 29, 2013 at 4:05
  • Your most recent edit caused your script to break, FYI. If it is null it will already be block, and thus break your code.
    – Jacques
    Sep 29, 2013 at 4:33
0

with JQuery .toggle() you can accomplish it easily

$( ".target" ).toggle();
1
  • 5
    i cant see jQuery tag... do you?
    – Ahsan Shah
    Sep 29, 2013 at 4:00
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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.