How to use the data from an INPUT in HTML and apply it to a variable in Javascript

Viewed 590

I'm trying to make a basic wallet program that will have my current value listed on the top. Whatever number that's put into the form, is then submitted which should hopefully then readjust the total to the new amount. Fairly straightforward and I feel like I am so close, but I just can't figure out what's missing.

It doesn't seem to be taking the data from the first input and running it through the function, maybe I need an ACTION? I've tried action, and even using new currentCash but it's not working ... could really use some help. Thanks so much!

     <html>
     <head> 
    <title>My Wallet</title>
<script type="text/javascript">
   currentCash = 40000;

  function deductFromTotal(){

  var spent = document.getElementById("money_spent");
 currentCash -= spent;
    return currentCash; } 
 </script>
</head>

    <body>
  <h2>Current total is: <script type="text/javascript">
    document.write(currentCash);
    </script> </h2>

  <input type="amount" size= 25 Placeholder="How much did you spend today?" ID="money_spent" >       
  <input type="button" value= "confirm" onClick="deductFromTotal()">

    </body>
    </html>
3 Answers

I have modified your question and tried to solve your issue.Hope it helps

  1. You were not taking the input's value

  2. You are using document.write ,you should select the element and change the text content

  3. You should use let ,var or const statements to declare your variables (A good practice to adopt)

You should also check if the entered value is number ,otherwise it returns NaN

let currentCash = 40000;

function deductFromTotal() {
  var spent = document.getElementById("money_spent").value;
  // check if the entered value is number
  if (isNaN(Number(spent))) {
    console.log("Numbers only")

  } else {

    //currentCash = currentCash - spent;
    currentCash -= spent;
    // give your h2 an id and then use getElementById ,better
    document.querySelector("h2").textContent = `Current total is:${currentCash}`;

  }
}
// deductFromTotal();
<html>

<head>
  <title>My Wallet</title>
</head>

<body>
  <input type="amount" size=2 5 Placeholder="How much did you spend today?" id="money_spent">
  <input type="button" value="confirm" onClick="deductFromTotal()">
  <h2>Current total is:
  </h2>
</body>

</html>

<html>
<head> 
<title>My Wallet</title>
</head>
<body>
    <h2>Current total is: <span id="result">40000</span> </h2>
    <input type="amount" size= 25 Placeholder="How much did you spend today?" id="money_spent" >       
    <input type="button" value= "confirm" onClick="deductFromTotal()">

<script type="text/javascript">
var currentCash = 40000;

function deductFromTotal(){

var spent = document.getElementById("money_spent").value;
document.getElementById('result').innerHTML = currentCash - spent; 
} 
</script>
</body>
</html>

You have an undeclared variable. currentCash -= money_spent; You really need to check your code better before posting it on stackoverflow, or the community will stick it to you.

Related