44

I have following DIV . I want to display the DIV after button click .Now it is display none

<div  style="display:none;" class="answer_list" > WELCOME</div>
<input type="button" name="answer" >
Community
  • 1
  • 1
Karthick Terror
  • 801
  • 2
  • 8
  • 11
  • 3
    http://stackoverflow.com/questions/4357291/javascript-show-element-on-click – pyvi Aug 05 '11 at 13:59
  • Unfortunately, every single answer here suggests `onclick` attributes. Please see [Show/hide 'div' using JavaScript](/q/21070101/4642212) instead. Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Jun 19 '21 at 06:43

4 Answers4

91

HTML Code:

<div id="welcomeDiv"  style="display:none;" class="answer_list" > WELCOME</div>
<input type="button" name="answer" value="Show Div" onclick="showDiv()" />

Javascript:

function showDiv() {
   document.getElementById('welcomeDiv').style.display = "block";
}

See the Demo: http://jsfiddle.net/rathoreahsan/vzmnJ/

Ahsan Rathod
  • 5,465
  • 2
  • 21
  • 25
16

HTML

<div id="myDiv" style="display:none;" class="answer_list" >WELCOME</div>
<input type="button" name="answer" onclick="ShowDiv()" />

JavaScript

function ShowDiv() {
    document.getElementById("myDiv").style.display = "";
}

Or if you wanted to use jQuery with a nice little animation:

<input id="myButton" type="button" name="answer" />

$('#myButton').click(function() {
  $('#myDiv').toggle('slow', function() {
    // Animation complete.
  });
});
James Hill
  • 60,353
  • 20
  • 145
  • 161
4
<script type="text/javascript">
function showDiv(toggle){
document.getElementById(toggle).style.display = 'block';
}
</script>

<input type="button" name="answer" onclick="showDiv('toggle')">Show</input>

<div id="toggle" style="display:none">Hello</div>
Terrik
  • 546
  • 4
  • 10
0
<div  style="display:none;" class="answer_list" > WELCOME</div>
<input type="button" name="answer" onclick="document.getElementsByClassName('answer_list')[0].style.display = 'auto';">
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    getElementsByClassName will return a whole set of items.. you need to declare which one you want to change like this getElementsByClassName('foo')[0].style.display... – Walialu Aug 05 '11 at 14:03
  • @James Hill: Do you want to declare a function for this case? var fooJames = function(id){var d = document.getElementById(id);if(d.style.display=='block')d.style.display='none';return; d.style.display='block'; } In germany we call this: Mit Kanonen auf Spatzen schießen! – Walialu Aug 05 '11 at 14:10
  • @Walialu, I understand what you're saying, but as a general rule, inline JS is poor practice. The OP probably has other JS running on the site. I placed my logic in a function so that it could be added to one of the .js files that he/she is already referencing. FYI - this is a good read: http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-a-bad-thing/. – James Hill Aug 05 '11 at 14:14
  • how to get the displayed content collapse on another click ? – akhil verma Jun 15 '15 at 07:51