简体   繁体   中英

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

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

What is the jQuery version you are using?

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

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

FIDDLE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM