45

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" >
2

4 Answers 4

92

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Ahsan Rathod , Thanks . How to display array Div usibng javascript .can u help me ? <div style="float:left; width:420px; padding:10px 0px;display:none;" class="answer_list" id="welcomeDiv" > <?php $query1= "select * from tblanswers where questionID ='$question' and productID='$product_id' "; $result1=mysql_query($query1); while($qwe=mysql_fetch_array($result1)) { ?> <input type="radio" name="answer[<?php echo $question; ?>]" value="<?php echo $qwe['answer_id']; ?>" /><?php echo $qwe['text']; ?> <br /> <?php } ?> </div>
It only shows the button but doesn't show the text when I click in
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.
  });
});

4 Comments

Your JQuery code is much more elegant that doing the same with document.getElementById - of course useful only when using JQuery :D
@MarcinCylke, great observation :P
if I do it with JS the block disappears after i reload the page but i want the box to stay until i explicitly close it. How I can do it?
It only shows the button but doesn't show the text when I click in
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>

Comments

0
<div  style="display:none;" class="answer_list" > WELCOME</div>
<input type="button" name="answer" onclick="document.getElementsByClassName('answer_list')[0].style.display = 'auto';">

4 Comments

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...
@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, 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: robertnyman.com/2008/11/20/….
how to get the displayed content collapse on another click ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.