/    Sign up×
Community /Pin to ProfileBookmark

print text from textbox to the same page

I wanted to get text from a textbox to display on the same screen. This is the code i have used but it doesn’t seem to work. The text prints to the screen but the textbox disappears and I cant enter anything else.
Any help?

<html>
<script language=’JavaScript’>

function getwords()
{
textbox = document.getElementById(‘words’);
if (textbox.value != “”)
document.write(“You entered: ” + textbox.value)
else
alert(“No word has been entered!”)
}

</script>
<input type=”text” id=”words” />
<input type=”button” onclick=”getwords()” value=”Enter” />

</body>
</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@LutinFouMay 10.2012 — Hi, try usng the innerHTML property

first create your output div in the body (i.e. <div id="output"> [I] Leave blank here[/I] </div>

Then in your function get create a variable that accesses the output div (i.e. var myOutput=document.getElementById('output').

Then instead of using the document.write method, just use myOutput.innerHTML="You entered: " + textbox.value;

in a nutshell:

<html>

<script language='JavaScript'>


function getwords(){

myOutput=document.getElementById('output');

textbox = document.getElementById('words');

if (textbox.value != "")

myOutput.innerHTML="You entered: " + textbox.value;

else

alert("No word has been entered!")

}

</script>

<input type="text" id="words" />

<input type="button" onclick="getwords()" value="Enter" />

<div id="output">

</div>

</body>

</html>
×

Success!

Help @bobodaman2003 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...