1

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

document.write (myTextField.value); 

1 Comment

getElementById() returns the actual element - in this case a textbox. Use .value to get the text within the textbox.
0
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.

2 Comments

I understand, I don't normally use document.write except tests. Although ya, an alert would still be better.
@JackstahComradDavis an alert is better than doc write, but installing firebug and using console.log would be even better.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.