Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing DIV visibility with JavaScript

EDIT: Since the question has became quite popular I will fix the problem so it will be working code example. The original problem is still listed, But the code works.

i am trying to show a div after pressing a button but this wont work, any idea why is that?

<form action="insert.php" method="POST" align="right" id="post_form">
<input type="button" value="click me" onClick="show()">
<div id="show_button">  fdsfds </div><br>
</form>

#show_button{
 visibility:hidden;
}

function show(){
  // alert("cheked the button - worked");
  document.getElementById("show_button").style.visibility= 'visible' ;
}
like image 431
antonpuz Avatar asked Apr 21 '13 14:04

antonpuz


People also ask

How do you make a division visible in JavaScript?

Another way to make a div visible or invisible is with the display property. Then we can make it visible with: const div = document. querySelector('div') div.

How do you make a div invisible in JavaScript?

The document. getElementById will select the div with given id. The style. display = "none" will make it disappear when clicked on div.

How do you make a div invisible and visible in HTML?

The hidden attribute hides the <div> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <div> element is not visible, but it maintains its position on the page.


2 Answers

Change your CSS to:

#show_button{
 display: none
}

And you in your javascript:

function show(){
  //alert("cheked the button - worked");
  document.getElementById('show_button').style.display= 'block' ;
}
like image 78
Dragos Rizescu Avatar answered Sep 21 '22 15:09

Dragos Rizescu


Typo error change it document.getElementById(show_button) to document.getElementById("show_button")

function show(){
  document.getElementById("show_button").style.visibility= "visible" ;
}
like image 30
Tamil Selvan C Avatar answered Sep 18 '22 15:09

Tamil Selvan C