Add two inputs if there is hidden div

1

In my programming I have two div with buttons to hide them and I have two inputs: nota1 and nota2 and I have a button to add those two inputs. I want those two buttons to be added only when the div1 is hidden.

Annex my code:

<!DOCTYPE html>
<html>
<head>
<title>Probando</title>

<script type="text/javascript">
function ocultar1() {
document.getElementById('nologrado1').style.display = "none";
}
</script>

</head>
<body>

<div id="nologrado1">
<h1>No logrado 1</h1>
<input type="submit" value="ocultar 1" onclick="ocultar1()">
</div>

<div id="nologrado2">
<h1>No logrado 2</h1>
<input type="submit" value="ocultar 2" onclick="ocultar2()">
</div>

<br><br><br>

<input type="text" placeholder="nota1">

<input type="text" placeholder="nota2">


<!--Quiero que este boton sume, la nota1 mas la nota 2, unicamente si el div logrado 1, esta ocultado.-->

<input type="submit" value="sumar" onclick="sumar()">

<!--Quiero que este boton sume, la nota1 mas la nota 2, unicamente si el div logrado 1, esta ocultado.-->

</body>
</html>
    
asked by Juan Andres Cesar Jimenez 30.10.2018 в 19:45
source

1 answer

1

You can achieve what you want like this:

<!DOCTYPE html>
<html>
<head>
<title>Probando</title>

<script type="text/javascript">
function ocultar(div) {
var id = div.parentNode.id;
document.getElementById(id).style.display = "none";
}
function sumar() {
  var input1 = parseInt(document.getElementById("input1").value);
  var input2 = parseInt(document.getElementById("input2").value);
  var div1 = document.getElementById("nologrado1");
  if(div1.offsetParent !== null) {
    document.getElementById("resultado").value = input1 + input2;
  }
}
</script>

</head>
<body>

<div id="nologrado1">
<h1>No logrado 1</h1>
<input type="submit" value="ocultar 1" onclick="ocultar(this)">
</div>

<div id="nologrado2">
<h1>No logrado 2</h1>
<input type="submit" value="ocultar 2" onclick="ocultar(this)">
</div>

<br><br><br>

<input id="input1" type="text" placeholder="nota1">

<input id="input2" type="text" placeholder="nota2">


<!--Quiero que este boton sume, la nota1 mas la nota 2, unicamente si el div logrado 1, esta ocultado.-->

<input type="submit" value="sumar" onclick="sumar()">
<br><br>
resultado: <input type="text" id="resultado">

<!--Quiero que este boton sume, la nota1 mas la nota 2, unicamente si el div logrado 1, esta ocultado.-->

</body>
</html>
    
answered by 30.10.2018 / 20:00
source