Qorzyking
Qorzyking

Reputation: 25

html input values in javascript

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?

Upvotes: 0

Views: 176

Answers (3)

etr
etr

Reputation: 1247

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

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

document.getElementById("value1").value

Upvotes: 0

Austinh100
Austinh100

Reputation: 598

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;.

Upvotes: 4

Daniel Bejan
Daniel Bejan

Reputation: 1468

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);
    }

Upvotes: 7

Related Questions