Reputation: 13
I want the text typed in textarea
with id text
in the div element typed
, how can i?
head
<script>
function field()
{
var txt = document.getElementById("text").value;
if (txt.length > 0){
document.getElementById("typed").value = txt;
}
}
</script>
body
<input type="text" id="text"></input>
<br>
<b> You Typed : <div id="typed"></div> </b>
<br>
<input type="button" value="Submit" onclick="field()">
Upvotes: 0
Views: 534
Reputation: 782735
.value
is for input elements. To put something in a DIV
, use .innerText
:
document.getElementById("typed").innerText = txt;
DEMO with <input>
DEMO with <textarea>
Upvotes: 3
Reputation: 6124
document.getElementById("typed").innerHtml = document.getElementById("text").value;
Upvotes: -2
Reputation: 713
you can with this :
<html>
<head>
<script>
function field()
{
var txt = document.getElementById("text").value;
if (txt.length > 0){
document.getElementById("typed").innerText = txt;
console.log(txt);
}
}
</script>
</head>
<body>
<input type="text" id="text"></input>
<br>
<b> You Typed : <div id="typed"></div> </b>
<br>
<input type="button" value="Submit" onclick="field()">
</body>
</html>
Upvotes: 0
Reputation: 3844
Change the function like the following
function field()
{
var txt = document.getElementById("text").value;
if (txt.length > 0)
{
document.getElementById("typed").innerText= txt;
}
}
Upvotes: 1