user3709688
user3709688

Reputation: 13

How to get the textarea value in a div element?

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

Answers (4)

Barmar
Barmar

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

Ankit Agrawal
Ankit Agrawal

Reputation: 6124

document.getElementById("typed").innerHtml = document.getElementById("text").value;

Upvotes: -2

Hann
Hann

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

Jesuraja
Jesuraja

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

Related Questions