Lemvig Kiggeren Lemvig Kiggeren - 6 months ago 24
jQuery Question

Hide HTML code temporarily and display them later

I am trying to remove all HTML tags from a textarea-tag (TEMPORARILY), but then display the textarea value after that on the page.

<textarea><span style="color: blue">Test</p></textarea><br />

Show textarea here: <span id="show"></span>


My jQuery

$("#show").html($("textarea").val());


http://jsfiddle.net/vQD5w/

That is how I show the textarea content at the moment, and that is how it is going to look in the end. Meanwhile I want to hide the HTML code from the textarea, so what is inside the textarea does not look ugly and unreadable to the common PC user. Textarea should therefore only contain "Test" (no quotations).

So basically I want to escape all HTML tags, elements, and everything related to HTML INSIDE the textarea-tag, but use it later on.

Answer

You could use regular expressions to strip out the tags, barring that you aren't trying to parse it.

$("#show").html($("textarea").val());
var text = $('textarea').val();
$('textarea').val(text.replace(/(<([^>]+)>)/ig,""));

Example: http://jsfiddle.net/brandonscript/ffC4q/