-2

So I am new to programming and want to create an app that takes the numerical value the customer inputs into and set them as my variables so they can be calculated?

How can I get the value from an input with JavaScript

For example a - b = c where a and b has been inputed in the app and c is the outcome of that calculation?

AnonymousSB
  • 3,516
  • 10
  • 28
  • Hi and welcome to SO. Your question is very unclear. Maybe try to get a better understanding of what exactly YOU want to program before asking (can be calculated = what EXACTLY do you want to calculate???). For stackoverflow it is best to illustrate a specific problem you're having in a [MCVE]. For more information, please see [Ask] and take the [Tour]. – quant Nov 19 '18 at 21:03
  • This is a duplicate of [How do I get the value of text input field using JavaScript?](https://stackoverflow.com/questions/11563638/how-do-i-get-the-value-of-text-input-field-using-javascript) – AnonymousSB Nov 19 '18 at 21:05
  • I apologize for the vagueness, with my inexperience here on stack overflow to javascript I am unsure really of how to ask the question properly. I am trying to create an app where the user inputs a numerical value and the beginning and end of there session and what those numbers to calculate. I have a skeleton of an app but I want to use. – Eliana Hebrew Nov 19 '18 at 22:25

2 Answers2

0

Here is an example where it automatically calculates when you type numbers into the fields.

It uses oninput to detect when the up/down arrows of the input field are pressed, and it uses onkeyup to detect when the user types numbers.

It will clear the result if either field is empty.

function sum() {
  var a = document.getElementById('a').value;
  var b = document.getElementById('b').value;
  var result = document.getElementById('result');
  
  if (!a || !b){
    result.innerHTML = '';
  } else {
    result.innerHTML = parseInt(a) + parseInt(b);
  }
}
<input id="a" type="number" oninput="sum()" onkeyup="sum()" /> +
<input id="b" type="number" oninput="sum()" onkeyup="sum()" /> =
<span id="result"></span>
AnonymousSB
  • 3,516
  • 10
  • 28
  • Awesome AnonymousSB I am trying to allow the user to edit an input and add a number and when they add that number it automatically calculates. For example they start off with a =5 and they edit the entry and add b=4. Once they put this info in the program autocalculate a-b or 5-4 and output a the results. – Eliana Hebrew Nov 19 '18 at 23:35
  • I updated my example, you want to fire `sum()` when the `onkeyup` event is fired. I also added a check to make sure both inputs had a value before calculating. – AnonymousSB Nov 19 '18 at 23:45
  • Okay cool . While I have your attention AnonymousSB do you know how to create an edit button that brings up information that has already been entered into the local forage database? I am on phone right now but will add the code when I get to the computer in the morning. But any thoughts would be great! – Eliana Hebrew Nov 20 '18 at 04:38
0
var a = Number(prompt("Enter first number"));
var b = Number(prompt("Enter second number"));
alert(a + " + " + b + " = " + (a + b));
Julia
  • 674
  • 1
  • 6
  • 18