I\'m having a problem trying to escape some code... Basically, I want to escape \"<\" and \">\" but I want them to APPEAR in my #output div as \"<\" and \">\". Current
A little different replace, but works for me (even with .html()
).
Demo
var str = $('#textarea').val();
$('#result').html(str.replace(/<|>/ig,function(m){
return '&'+(m=='>'?'g':'l')+'t;';
}));
<textarea id="textarea">
Hello, <b>World</b>!
</textarea>
<div id="result"></div>
(This is just to verify it can be done, .text()
is the better approach)
Try this:
var textval = $("#textarea").val();
$("#output").text(textval);
jQuery offers two methods - $.text() and $.html() where the method names speak for themselves :)