JavaScript Entries

0

It's my first post here, and I'd like to ask you a question that would solve my life with a project. I am developing a web page in HTML, CSS and JS, in this, I have a kind of "Virtual Calculator". My idea was to ask the user to enter some values, and based on them, show a result. I know that is done with JavaScript, but I do not know how to do it so that it takes the values entered by the user, adds them, and returns a result. For example.

Enter number 1 = (Here the user has a text box where he puts, for example, the number 2) Enter number 2 = (idem) That there is a button that says "solve", or is done alone, is not the main point, and that the The result is = 4;

I was searching the internet, but most of them show it to you with the sign that appears above, but I would like it to be on the same page.

since thank you very much!

    
asked by Denu Lemos 30.11.2017 в 18:01
source

2 answers

1

You can do it in the following way:

window.addEventListener('load', main, false);

function main() {

  var na = document.querySelector('#numA');
  var nb = document.querySelector('#numB');
  var calc = document.querySelector('#calc');
  var res = document.querySelector('#result');

  calc.addEventListener('click', function() {
    res.innerHTML = 'La suma es ' + (parseFloat(na.value) + parseFloat(nb.value));
  }, false);

}
<input id="numA">
<input id="numB">
<button id="calc"> Sumar </button>
<span id="result"></span>

To try it click on the "Run" button below.

    
answered by 30.11.2017 / 18:43
source
0

Assuming you have two numeric input and one submit type

<input type="number" id="numero-uno">
<input type="number" id="numero-dos">
<input type="submit" id="resolver" value="Resolver">

First you refer to the html elements

let inputUno = document.getElementById('numero-uno');
let inputDos = document.getElementById('numero-dos');
let inputResolver = document.getElementById('resolver');

Then you make a function so that when you click on the solve button, save the values.

inputResolver.addEventListener('click', function() {
  let numeroUno = inputUno.val();
  let numeroDos = inputDos.val();
  let resultado = numeroUno + numeroDos;
  alert(resultado);
});
    
answered by 30.11.2017 в 18:14