How do I make a JavaScript that show / hide an HTML div?
I tried type:
function Mudarestado(divid)
{
var disp = document.getElementById(divid).style.display;
disp = "none; // (ou disp = "block")
}
But that did not work.
How do I make a JavaScript that show / hide an HTML div?
I tried type:
function Mudarestado(divid)
{
var disp = document.getElementById(divid).style.display;
disp = "none; // (ou disp = "block")
}
But that did not work.
The solutions below will provide a way of how to implement in a variety of situations.
Pure Javascript
function Mudarestado(el) {
var display = document.getElementById(el).style.display;
if(display == "none")
document.getElementById(el).style.display = 'block';
else
document.getElementById(el).style.display = 'none';
}
<div id="minhaDiv">Conteudo</div>
<button type="button" onclick="Mudarestado('minhaDiv')">Mostrar / Esconder</button>
Solution in JQuery
$(function(){
$(".btn-toggle").click(function(e){
e.preventDefault();
el = $(this).data('element');
$(el).toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="minhaDiv">Conteudo</div>
<button type="button" class="btn-toggle" data-element="#minhaDiv">Mostrar / Esconder</button>
Angle JS
angular.module("ExemploApp", [])
<body ng-app="ExemploApp">
<div id="minhaDiv" ng-init="MinhaDiv = true" ng-show="MinhaDiv">Conteudo</div>
<button type="button" ng-click="MinhaDiv = !MinhaDiv">Mostrar / Esconder</button>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</body>
Your variable disp
is only getting a COPY of the current value of the "display" attribute. Changing the value will not change the value of this "display" attribute.
So its function should look like this:
function Mudarestado(divid)
{
document.getElementById(divid).style.display = "none"; // ou "block"
}
If you want a function that shows when it is hidden, and hide when it is visible (ie a toggle ), do so:
function toggleEstado(divid) {
var div = document.getElementById(divid);
var disp = div.style.display;
div.style.display = disp == 'none' ? 'block' : 'none';
}
Maybe it's just some syntax errors. Here's an example below:
document.getElementById("minhadiv").style.display = 'none';
In this case, minhadiv is the div id.
JavaScript is very powerful and we can enjoy various language features
As stated in one of the previous answers, your disp variable is getting the display value
function Mudarestado(divid) {
var disp = document.getElementById(divid).style.display;
disp = "none"; // (ou disp = "block")
}
Following your thinking, you would have to change your function so that * variable disp * receives the element rather than the value of an attribute of it, you can do this as follows:
var disp = document.getElementById(divid)
Then you would receive the element and then yes it could work on its attributes, disp.style.display = 'none'
or disp.style.display = "none"
because JavaScript understands both single quotes and double quotes like String.
Using jQuery , the function would look something like this:
$('ID_DA_DIV').hide();
If you want to already load invisible use CSS for this using display: none
.
With jQuery
$('ID_DA_DIV').css("display", "none")
You can also use the setProperty
method of the CSSStyleDeclaration
interface, for example:
document.querySelector("element").style.setProperty("display", "none", "important");
The last parameter is optional.
Very simple, I did it with a button, but it can be done in the div itself
<script>
function Mudarestado(divid){
document.getElementById(divid).style.display = "none";
}
</script>
<html>
<div id=1>aaa</div><input type="button" onclick="Mudarestado(1)">
</html>
onclick
na div
<script>
function Mudarestado(divid){
document.getElementById(divid).style.display = "none";
}
</script>
<html>
<div id=1 onclick="Mudarestado(1)">aaa</div>
</html>
Using the class name as a reference:
function ocultaElemento(el) {
var display = document.getElementsByClassName(el);
if(display[0].style.display == "none")
display[0].style.display = 'block';
else
display[0].style.display = 'none';
}