2
Hello,
I am facing a difficulty with my javascript code, because I can’t imagine a way in which it is possible to access the constructor functions of my Javascrip object in the HTML form.
I wonder if there’s a way to do it that I’m wanting.
Below follows in attachment the code HTML and Javascript, respectively.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Calculadora</title>
<meta charset="UTF-8">
<script src="biblioteca.js" type="text/javascript" ></script>
</head>
<body >
<form id="formulario">
<p>Digite o primeiro número</p>
<input type="text" name="valor1" value="0">
<p>Digite o segundo número</p>
<input type="text" name="valor2" value="0">
<p>Resultado</p>
<input type="text" name="resultado" disabled="disabled">
<br/>
<br/>
<table>
<caption>Operações</caption>
<tr>
<th><a href="#" onclick="Calculadora().getSoma()">SOMAR</a></th>
<th><a href="#" onclick="Calculadora().getSubtracao()">SUBTRAIR</a></th>
</tr>
<tr>
<th><a href="#" onclick="Calculadora().getMultiplicacao()">MULTIPLICAR</a></th>
<th><a href="#" onclick="Calculadora().getDivisao()">DIVIDIR</a></th>
</tr>
</table>
</form>
</body>
</html>
Javascript:
function Calculadora(){
this.setValor1 = function(numero1){
this.valor1 = +document.getElementById('formulario').valor1.value;
}
this.setValor2 = function(numero2){
this.valor2 = +document.getElementById('formulario').valor2.value;
}
this.getSoma = function soma(){
resultado = this.valor1 + this.valor2;
document.getElementById('formulario').resultado.innerHTML = resultado;
}
this.getSubtracao = function subtracao(){
resultado = this.valor1 - this.valor2;
document.getElementById('formulario').resultado.innerHTML = resultado;
}
this.getMultiplicacao = function multiplicacao(){
resultado = this.valor1 * this.valor2;
document.getElementById('formulario').resultado.innerHTML = resultado;
}
this.getDivisao = function divisao(){
resultado = this.valor1 / this.valor2;
document.getElementById('formulario').resultado.innerHTML = resultado;
}
}
From now on, thank you.
Why doesn’t onclick javascript?
– Guilherme Lopes