Guffas
Guffas

Reputation: 9

Make Javascript calculate HTML input from user

I want to make a website that can calculate the numbers that the user enters in the text field. Below, I am trying to store the users input in a variable and then return it to the console, but this does not seem to work. It is supposed to take in more calculations down the line, but I thought I would keep it simple at first. What have i forgotten?

PS: I'm currently still learning JavaScript, so don't roast me too hard :)

Thanks!

Best Mikkel

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="/JavaScript/culjo.js" defer></script>
        <title>Culjo</title>
    </head>
    <body>
    <h1>Culjo</h1>

    <input id="inputOne" type="text">
    <input id="inputtwo" type="text">
    <input id="result" type="number">





    <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256- 
    QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
        <script 
    src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    </body>
    </html>

Javascript

        function calculate() {
        var inputOne = document.getElementById("inputOne")
        var inputTwo = document.getElementById("inputTwo")
            result = inputOne+inputTwo + inputTwo
            
            return result
    }
    calculate()

Upvotes: 0

Views: 1164

Answers (1)

panbak
panbak

Reputation: 416

You need to get the value of the field like this:

var inputOne = document.getElementById("inputOne").value;

and then use

parseInt() or parseFloat() as stated at the comments to parse the string.

Upvotes: 1

Related Questions