-3

Photo of the website

I have a school project and I need to make a website where I can add more balance to my account. But I can't figure it out.

I would like to make an HTML number input and then that input needs to add up with my existing balance. But I can't make it work

This is the code I have:

<fieldset>
     <legend>Opwaarderen</legend>
     <input type="number" id="opwaarderen">
     <input type="submit" onclick="opwaarderen()">
</fieldset

But how can I make the Javascript function? So that the input will add up to my already existing balance.

Erikction
  • 1
  • 3
  • 1
    Possible 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) – Fallen Jan 12 '19 at 20:31
  • @Erikction Where are you displaying the balance or storing it? – Ryan Wilson Jan 12 '19 at 20:32
  • Which part of this are you having trouble with? Getting the value from the input, or adding it to your existing balance? The most common problem is forgetting to convert the input to a number, so you end up concatenating instead of adding. – Barmar Jan 12 '19 at 20:33
  • Did your teacher really assign a project without teaching the basics you need to solve it? Do you have a textbook? – Barmar Jan 12 '19 at 20:34
  • We never learn out of books, they always tell us to go to w3schools. I can't figure out how to make a function that gets the input from the HTML page and make that into a function that adds up the input with the already existing variable. So I can add more money in my balance on the site via the input box. – Erikction Jan 12 '19 at 20:44
  • @Erikction `.value` will get, just like it implies, the value of the input. As Barmar said, you then convert that to a number and add to your balance variable. To get the input element you can use `document.getElementById("opwaarderen");` – Ryan Wilson Jan 12 '19 at 20:49

1 Answers1

1

This will help

var currentCredit=0;

$("#a").click(()=>
{
alert(currentCredit+=Number($("#opwaarderen").val()));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset>
     <legend>Opwaarderen</legend>
     <input type="number" id="opwaarderen">
     <input type="submit" id="a">
</fieldset>
ellipsis
  • 12,049
  • 2
  • 17
  • 33