0

I can't seem to figure out this problem that I have:

<script>
    function getValues(){
        var value1 = document.getElementById("value1");
        var value2 = document.getElementById("value2");
        var value3 = document.getElementById("value3");
        alert(value1 +" "+ value2 +" "+ value3);
    }
</script>

<p>value1</p>   
<input type="text" id='value1' />
<p>value2</p>
<input type="text" id='value2' />
<p>value3</p>
<input type="text" id='value3' />
<input type="button" value="submit" onclick="getValues()" />

I try to get the values the user filled in and then alert them back to them but instead of showing the values they filled in, it says:

[object HTMLInputElement] [object HTMLInputElement] [object HTMLInputElement]

Does anyone have an idea what could be the problem?

3 Answers 3

7

Try it like this

function getValues(){

        var value1 = document.getElementById("value1").value;
        var value2 = document.getElementById("value2").value;
        var value3 = document.getElementById("value3").value;
        alert(value1 +" "+ value2 +" "+ value3);
    }
Sign up to request clarification or add additional context in comments.

Comments

4

var value1 = document.getElementById("value1");

Gets the DOM Element associated with the value1 Id. In order to get the value of the element you need to do:

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

1 Comment

the explanation of why the .value has to be added helped me understand it better. Thank you.
0

document.getElementById("value1")returns a dom element.

To assign it, get the value by using it like this:

document.getElementById("value1").value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.