Jackstah Comrad Davis
Jackstah Comrad Davis

Reputation: 13

getElementById Not Working Right

Ok, so I'm REALLY new to programming and javascript, but I'm having a problem with this little string of code. The thing that is bothering me about it, is that I have done things similar to this in other programs, but it's just not working right in this specific little part of this program. Here is basically what isn't working:

<html>

<script type="text/javascript">

function test()
{
var myTextField = document.getElementById('myText');
document.write (myTextField);
}

</script>


<form>
<input type="text" id="myText">
<input type="submit" value="submit" OnClick="test()">
</form>
</html>

When I do this, it returns [object HTMLInputElement] instead of the value of that text field. Thanks for any help cause I'm most of you know this. :P

Upvotes: 1

Views: 109

Answers (3)

Howard
Howard

Reputation: 3895

getElementById returns the Object itself, which has many methods and properties as members.

You need to reference the value property, like this:

document.getElementById('myText').value;

That should work :)

Also, here's a general reference: https://developer.mozilla.org/en/A_re-introduction_to_JavaScript

Upvotes: 1

Cheery
Cheery

Reputation: 16214

function test()
{
var myTextField = document.getElementById('myText').value;
alert(myTextField);
// or 
console.log(myTextField);
}

You should not use document.write here, as you document is already loaded. Document.write will remove the page.

Upvotes: 0

MazepiC
MazepiC

Reputation: 335

Try:

document.write (myTextField.value);

Upvotes: 0

Related Questions