Oleh Kurpiak
Oleh Kurpiak

Reputation: 1369

Set html text into textarea js

I have a textarea where i need to set some html text

<textarea id="text"></textarea>
<div id="elem">
  <p>text</p>
  <hr />
  <br />
  <p> some text</p>
</div>
..
$('#text').val($('#elem').html());

when I do this it's put text with html tags instead of parse it and put like a plain text. i also tried functions append(), html(), text(). none of them helps me

Upvotes: 0

Views: 910

Answers (3)

Shambhavi
Shambhavi

Reputation: 335

text() worked for me. See https://jsfiddle.net/mgLp90em/.

What is the jQuery version you are using?

Upvotes: 1

leo.fcx
leo.fcx

Reputation: 6467

I'd recommend to use a contenteditable div element instead of a textarea as follows:

<div class="text-area" contenteditable>
    <h1>Your content here</h1>
</div>

See JSfiddle demo

Upvotes: 0

Michelangelo
Michelangelo

Reputation: 5958

This should do the trick, gets only text from the children within your #elem:

$('#text').val($('#elem').children().text());

FIDDLE

Upvotes: 0

Related Questions