views:

401

answers:

3

Hello, Maybe topic really doesn't explain my problem correctly. I'll try to explain more precise it here. So I'am having a <textarea> and <p> aragraph tags. If I'am entering some text in textarea, and then after button click I insert textarea value into paragraph html. If its text everything is fine, but if it's a hand written html code, its not right, because I don't want browser to parse that html code. Here is some simple example involved jQuery:

$('.some_button').click(function () {
$('p').html($('textarea').val());
});

So how I can insert html code as text to paragraph? Thanks in advance!

+3  A: 

With the text method.

David Dorward
Not exactly - text() just strips off any HTML and leaves the text content only. If I understand the OP correctly, he wants to see the actual HTML source code.
Pekka
@Pekka - the jQuery documentation says otherwise: "We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), which replaces special characters with their HTML entity equivalents (such as < for <)"
David Dorward
@David you're right, sorry. +1
Pekka
+3  A: 

You need to escape the HTML:

$('p').html($('textarea').val().replace(/</g,'&lt;').replace(/>/g,'&gt;'));

EDIT: use .text() instead as already mentioned unless you want to do a manual conversion.

David
David Dorward
David
There is more to HTML than 'tags'. Just wait until the data includes the string `©` — you'll end up displaying a copyright symbol instead of that string (ditto any other genuine HTML entity).
David Dorward
+1  A: 

David Dorward answered this already, and I'd have added a comment instead of an answer but wasn't sure the code would show up right, so.. With this

$('.some_button').click(function () {
  $('p').text($('textarea').val());
});

and

<input type='button' class='some_button' value='click!'><br>
<textarea>foo&lt;br&gt;foo</textarea>
<p></p>

Clicking the button shows "foo<br>foo" in the paragraph. Is that what you need?

--Addendum--

Ok.. I am only a starting JavaScripter, so not sure how bad this is :-) But the following replaces special characters by their HTML equivalents while replacing linebreaks with a <br>.

<script type="text/javascript">
String.prototype.htmlents = function() {
  var trans = {'&':'&amp;', '"':'&quot;', '\'':'&#039;', '<':'&lt;', '>':'&gt;'};
  var arr = this.split('');
  for (var i=0;i<arr.length;i++) {
    if (trans.hasOwnProperty(arr[i])) {
      arr[i] = trans[arr[i]];
    }
  }
  return arr.join('');
};

$(document).ready(function(){
  $('.some_button').click(function () {
    $('p').html($('textarea').val().htmlents().replace(/\n/g,'<br>\n'));
  });
});
</script>

Remembered I've had to do something similar before, and still had the htmlents() lying around from that time. Feel free to criticise :-)

MSpreij
Textarea elements are defined as containing PCDATA, so that needs to be `>br` not `<br`
David Dorward
Ah, right you are, fixed, thanks.
MSpreij