0

I have no idea how to get value from input box. Right now when I click BET button it just subtracts 100, I want to achieve so when I enter the value in the text box and hit bet it'll subtract the value which I've entered from the balance. Here is my code:

HTML:

<div>
    <input type="text" value=0 name="betAmount">
    <button class="betBTN">BET</button>
</div>

JS:

document.addEventListener('DOMContentLoaded',function() {
document.querySelector('.betBTN').addEventListener('click', function() {
      var toAdd = document.querySelector('div').textContent - 100;
      document.querySelector('div').innerHTML = "";
      document.querySelector('div').insertAdjacentHTML('afterbegin', toAdd);
    });
});

Any suggestions?

Siguza
  • 21,155
  • 6
  • 52
  • 89
Matt
  • 37
  • 4
  • What have you tried? This should be a quick Google query. https://www.google.com/?q=Get+value+from+input+box+with+js – Sid Apr 09 '17 at 11:35

2 Answers2

2

Replace your 100 with

document.querySelector("input").value;

This line will get the value of input element

document.addEventListener('DOMContentLoaded',function() {
document.querySelector('.betBTN').addEventListener('click', function() {
      var toAdd = document.querySelector('div').textContent - document.querySelector("input").value;
      document.querySelector('div').innerHTML = "";
      document.querySelector('div').insertAdjacentHTML('afterbegin', toAdd);
    });
});
Balance: <div>1000</div>
    <input type="text" value=0 name="betAmount">
    <button class="betBTN">BET</button>
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
0

If you’re talking about the entered value, then the following would do the job:

document.querySelector('input[name="betAmount"]').value;

All input elements, whether they are HTML input or textarea or select etc have a value property which is the actual value of the user data.

Manngo
  • 14,066
  • 10
  • 88
  • 110