0

I am new to Javascript and am having some difficulty. I want to have the user type in a word and have a function identify the word and print it back to the user on the same page. Everytime I run this code though it takes me to a DIFFERENT page AND prints '[object]'. Here is my code. Can someone please help me.

Live Long and Prosper.

<html>
<head>
<script src="raphael-min.js"></script>
<script src="jquery-1.7.2.js"></script>

<script type="text/javascript">
function animate() {
var txt = document.getElementById("words");
document.write(txt);
}
</script>
</head>
<body>

Text: <input type="text" id="words" value="" />
<input type="submit" value="Animate" onclick="animate()" />

</body>

</html>

3 Answers 3

3

Add a div to your page

<div id='msg'></div>

and change your js function to

 <script type="text/javascript">
    function animate() {
    var txt = document.getElementById("words").value;
    document.getElementById("msg").innerHTML = txt;
    return false;
    }
    </script>

Also make the following change to your submit button

<input type="submit" value="Animate" onclick="return animate()" />
Sign up to request clarification or add additional context in comments.

3 Comments

haha patience clyde. I have one last question and you will recieve your accepted answer. --What is the purpose of 'return false;'? Thanks, Spock
@user1460876 : That was just a suggestion :P
return false when used with the submit button prevents your form from submitting. I used it coz i thought you have a form. Not relevant in your case.
0

This code:

var txt = document.getElementById("words");

gives you an input, i.e. DOM object (which is represented by [object] when turned to string). And you want to retrieve its content, i.e. value. Try this:

var txt = document.getElementById("words").value;

Refreshing is caused by the submit button. Change its type to type="button".

Comments

0

Instead of

    function animate() {
       var txt = document.getElementById("words");
       document.write(txt);
   }

try with

   function animate() {
       var txt = document.getElementById("words").value;
       document.write(txt);
   }

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.