To make variables available to all functions, declare them in the enclosing scope of the functions - that is, in this case, declare them at the top of the page-ready (loaded) event handler.
This full demonstration include the advice I mentioned in my comment on the question; it uses the DOMContentLoaded
event rather than onload
, and it adds event listeners, where .onload = ...
and onclick = ...
blindly assigns event handlers, possibly replacing an existing event handler.
This is a fully complete .html
file which you can save and load into your browser:
<!DOCTYPE html>
<html>
<head>
<title>SO 46457061</title>
<script>
document.addEventListener('DOMContentLoaded', function(event) {
// Get the two number and one answer elements once and store them,
// saving us from having to traverse the DOM every time.
// Declaring these here makes them available to all code within
// this anonymous event-listener function.
var n1el = document.getElementById('n1');
var n2el = document.getElementById('n2');
var resultEl = document.getElementById('result');
// Attach a click-event handler to each of the buttons,
// providing an anonymous function as the handler.
document.getElementById('soma')
.addEventListener('click', function(e) {
e.preventDefault();
// Log just to demonstrate
console.log('n1 = ' + n1.value);
console.log('n2 = ' + n2.value);
// Compute
resultEl.innerText = '' +
(parseFloat(n1.value) +
parseFloat(n2.value));
});
document.getElementById('subtracao')
.addEventListener('click', function(e) {
// Stop what normally happens on a button-click, and...
e.preventDefault();
// ...instead set the text of the answer to the computed value
// This performs the operation on the numbers, as numbers,
// but adds a string at the start to force the result to be
// a String. This is not strictly necessary, since assigning
// it to .innerText also converts to a String.
resultEl.innerText = '' +
(parseFloat(n1.value) -
parseFloat(n2.value));
});
document.getElementById('multiplicacao')
.addEventListener('click', function(e) {
e.preventDefault();
resultEl.innerText = '' +
(parseFloat(n1.value) *
parseFloat(n2.value));
});
document.getElementById('divicao')
.addEventListener('click', function(e) {
e.preventDefault();
resultEl.innerText = '' +
(parseFloat(n1.value) /
parseFloat(n2.value));
});
});
</script>
<style>
button {
height: 1.5em;
width: 1.5em;
font-size: 20px;
margin-bottom: 5px;
}
section {
display: inline-block;
}
</style>
</head>
<body>
<section id="numbers">
<label for="n1">First Number</label> <input type="text" id="n1" name="n1"> <br>
<label for="n2">Second Number</label> <input type="text" id="n2" name="n2"> <br>
<span class="result">Result: <span id="result">_</span></span>
</section>
<section id="operators">
<button id="soma">+</button>
<button id="subtracao">−</button> <br>
<button id="multiplicacao">×</button>
<button id="divicao">÷</button>
</section>
</body>
</html>
Same thing, basically, but as a stack-snippet so you can see it in action right here.
document.addEventListener('DOMContentLoaded', function(event) {
// Get the two number and one answer elements once and store them,
// saving us from having to traverse the DOM every time.
// Declaring these here makes them available to all code within
// this anonymous function.
var n1el = document.getElementById('n1');
var n2el = document.getElementById('n2');
var resultEl = document.getElementById('result');
// Attach a click-event handler to each of the buttons,
// providing an anonymous function as the handler.
document.getElementById('soma')
.addEventListener('click', function(e) {
e.preventDefault();
resultEl.innerText =
parseFloat(n1.value) +
parseFloat(n2.value);
});
document.getElementById('subtracao')
.addEventListener('click', function(e) {
e.preventDefault();
resultEl.innerText =
parseFloat(n1.value) -
parseFloat(n2.value);
});
document.getElementById('multiplicacao')
.addEventListener('click', function(e) {
e.preventDefault();
resultEl.innerText =
parseFloat(n1.value) *
parseFloat(n2.value);
});
document.getElementById('divicao')
.addEventListener('click', function(e) {
e.preventDefault();
resultEl.innerText =
parseFloat(n1.value) /
parseFloat(n2.value);
});
});
button {
height: 1.5em;
width: 1.5em;
font-size: 20px;
margin-bottom: 5px;
}
section {
display: inline-block;
}
<section id="numbers">
<label for="n1">First Number</label> <input type="text" id="n1" name="n1"> <br>
<label for="n2">Second Number</label> <input type="text" id="n2" name="n2"> <br>
<span class="result">Result: <span id="result">_</span></span>
</section>
<section id="operators">
<button id="soma">+</button>
<button id="subtracao">−</button> <br>
<button id="multiplicacao">×</button>
<button id="divicao">÷</button>
</section>