Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting inner html without escaping or decoding anything with jQuery

Tags:

html

jquery

I am developing some kind of online html editor and I have a problem with getting the content of a textarea with jQuery.

The Textarea element is like this:

<textarea id="myText"><b>&lt;p&gt;TEST&lt;/p&gt;</b></textarea>

So it is a mixture of escaped and unescaped text. However, the browser shows the textarea like this, which is also something that I don't want :

<b><p>TEST</p></b>

I want to get the inner html of the textarea as it is. If I try .html(), it escapes all the tags and if I try .val(), it decodes the escaped tags. You can see the behavior at http://jsfiddle.net/hnBte/

How can I get the pure content with Javascript and optionally, how can I make the textarea show the content as it is?

Thanks

like image 452
Ahmet Eren Başak Avatar asked Apr 27 '13 13:04

Ahmet Eren Başak


1 Answers

I found this a really interesting question, so I did some research: I tried all javascript and jQuery functions to see what I would get, but nothing worked. You can only get completely unparsed or completely parsed html.

I also stumbled upon this SO-question, that also confirms that, sadly, it just can't be done: Getting raw text content of HTML element with HTML uninterpreted.

like image 143
Terrabythia Avatar answered Nov 15 '22 03:11

Terrabythia