JQuery HTML Exclusion from Text Area

I want to avoid HTML tags for entity names by taking text from textarea and putting the result in a second textarea so that:

 <mytag> 

becomes

 &lt;mytag&gt; 

I use .html() and .text() in the reverse order and forward. My problem is with the textarea element, which acts a little differently.

It works fine if I first put the text in a div:

 var htmlStr = $('#textareaInput').val(); //doesn't like .html() .text() ? $('#dummy').text(htmlStr); // an object to hold the text that supports .html() $('#textareaOutput').val($('#dummy').html()); 

But I want to do something more straightforward:

 var htmlStr = $('#textareaInput').val(); $('#textareaOutput').val($(htmlStr).html()); 

I think my problem is that I don’t understand how to manipulate jQuery objects, such as strings, without manipulating DOM elements, because right now I am using a div because it has a .html() method.

Any help would be fantastic!

Thanks.

+7
jquery html textarea entities
source share
1 answer

to try

 var htmlStr = $('#textareaInput').val(); $('#textareaOutput').val($('<div/>').text(htmlStr).html()); 
+5
source share

All Articles