How do I get the values of the text inputs and add them? [duplicate]

  html, javascript

I’ve been trying to get what I typed inside 3 text inputs and join them together. How would I go around doing this?

I’ve tried this and have had no luck.

<!DOCTYPE html>
<html>
    <div>
        <form>
            <div>
                <label>Number </label>
                <input type="text" id="dataElement1">
            </div>
            <br>
            <div>
                <label>First Name </label>
                <input type="text" id="dataElement2">
            </div>
            <br>
            <div>
                <label>Last Name </label>
                <input type="text" id="dataElement3">
            </div>
            <br>
            <script>
                var dataElement1 = document.getElementById("dataElement1").value
                var dataElement2 = document.getElementById("dataElement2").value
                var dataElement3 = document.getElementById("dataElement3").value
        
                function dataGet() {
                    const fullData = dataElement1 + dataElement2 + dataElement3
                    document.getElementById("output").innerHTML = fullData
                }
            </script>
            <button type="button" onclick="dataGet()">Get Data</button>
            <h2>Data:</h2>
            <p id="output">None</p>
        </form>
    </div>
</html>

Source: Ask Javascript Questions

LEAVE A COMMENT