views:

89

answers:

1

I want to escape HTML tags to entity names, taking text from a textarea and putting the result in a second textarea such that:

<mytag> becomes &lt;mytag&gt;

I'm using .html() and .text() going back and forth OK. My problem is dealing with the textarea element, which acts a little different....

It works fine if I first place the text into 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 like this:

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

I guess my problem is that I don't understand how to manipulate jQuery objects, like strings, without manipulating DOM elements -because right now I'm using a div because it has the .html() method.

Any help would be fantastic!

Thanks.

+1  A: 

try

var htmlStr = $('#textareaInput').val(); 
$('#textareaOutput').val($('<div/>').text(htmlStr).html());
Reigel
+1 You were quicker than I was, but I don't think your second solution would work. It would give you just the text content. http://jsfiddle.net/wtXBg/1/
patrick dw
Wow! Thanks...can't say I totally understand how it works yet, but it works! ( the first one)Thank you!
Wayne
@patric - can I ask you something?... my reputation score is a little bit awkward lately... sometimes, it will not add the up-votes?... is there a bug here at SO?
Reigel
@Reigel - If you've received the maximum daily points for up-votes (200), then you will not get points until you reach a 24 hour period where you are below the maximum. You'll still get points for accepted answers, though. For more info, check out http://meta.stackoverflow.com/ :o)
patrick dw
Reigel, I don't know of any problems; check your [reputation audit](http://stackoverflow.com/reputation); at the very bottom ("total rep") it shows you how much rep you would have after a recalc, i.e. how much rep you should *really* have. If you want a recalculation, just ask for one (using the "flag for moderator" button). See http://meta.stackoverflow.com/questions/43004/how-do-i-audit-my-reputation for more info on the rep audit.
balpha
@balpla as you can see here in this image, http://img820.imageshack.us/img820/1948/stackoverflow.jpg (reputation for this week) my rep are not properly calculated... for now it's not that much... But I'm just afraid that this will go on like this...
Reigel
@Reigel I did a recalc on your rep. You won't be happy about it. I know I wasn't when I did a recalc of my own rep. Your reputation should be calculated correctly now. If it isn't, then nobody's rep is correct, and so it doesn't matter (precision vs. accuracy; if accuracy is off for one, its off for everybody--but as rep is calculated the same for everyone the numbers are precise).
Will
@balpla, and now after getting the reputation audit, my rep from 7,129 went down to 6,924... lol
Reigel
@Will yeah... no worries... :D
Reigel
@patrick thank you for the info... I got it now...
Reigel
What you see on that image you posted is the result of the daily reputation cap -- you can only gain 200 points per day through votes.
balpha
@balpla, @balpla, Thank you for explaining... :)
Reigel