最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Escape HTML tag in <textarea> - Stack Overflow

programmeradmin2浏览0评论

I have one <textarea> tag in my website where the user can put there HTML code and see its preview. My problem is when the user enter code below mention in my <textarea> my preview functionality getting fail :

<html>
<textarea>Some code to show</textarea>
</html>

So question is how can I escape this html code in my <textarea> tag as I know the problem is ing because </textarea> tag.

Any solution on this please.

Edit

Question is about using </textarea> within a textarea.

Problem visible here: /

I have one <textarea> tag in my website where the user can put there HTML code and see its preview. My problem is when the user enter code below mention in my <textarea> my preview functionality getting fail :

<html>
<textarea>Some code to show</textarea>
</html>

So question is how can I escape this html code in my <textarea> tag as I know the problem is ing because </textarea> tag.

Any solution on this please.

Edit

Question is about using </textarea> within a textarea.

Problem visible here: http://jsfiddle/hrP6F/

Share Improve this question edited Jul 24, 2014 at 10:52 Ron van der Heijden 15.1k7 gold badges62 silver badges86 bronze badges asked Jul 24, 2014 at 10:37 user3243855user3243855 3534 silver badges10 bronze badges 8
  • 1 huh? - can't understand your problem mate – Ruben Serrate Commented Jul 24, 2014 at 10:38
  • 1 Can't you just use a div or something with contenteditable attribute? – putvande Commented Jul 24, 2014 at 10:39
  • see i have one textarea in that user can past his html code now that html code can also contain <textarea> tag so because of that tag it is showing me some of text outside the text box – user3243855 Commented Jul 24, 2014 at 10:40
  • 1 stackoverflow./questions/3353129/… – Bhojendra Rauniyar Commented Jul 24, 2014 at 10:41
  • @putvande yes your right i can use that too !! Thanks :) – user3243855 Commented Jul 24, 2014 at 10:42
 |  Show 3 more ments

2 Answers 2

Reset to default 3

EDIT: for your purpose this would do:

<textarea>
        Outside Textarea
        &lt;textarea&gt;Inside Textarea&lt;/textarea&gt;
</textarea>

source: How can I embed a textarea inside of another textarea in HTML?

Or use contenteditable like someone already mentioned -> click

FIRST ANSWER: Im not sure I understand perfectly but still. You want to display the code inside text area somewhere else for instance?

You could do that on click like this (I reckon you are not statically putting nested text areas in html?):

HTML:

<textarea id="textarea" >Something code to show</textarea>
<button onclick="show()">show</button>
<div id="showArea"></div>

JS:

function show(){
    var t = document.getElementById('textarea').value; 
    document.getElementById('showArea').innerHTML = t;
}

This is of course if what you want is to display html that is inside textarea. you could also put another textarea inside first one and it will work.

If you want the results to display dynamically you could use

<textarea id="textarea" onkeyup="show()">Something code to show</textarea>

This works even if you put your code (html and text area) inside text area - it displays it, I tested it

You can add a output div for preview purpose. Below is the jQuery script

HTML

<textarea placeholder="Enter your html"><b>test</b></textarea>
 <a href="#" class="run">Run</a>
  <div class="op"></div>

JS

 $('.run').click(function(){
    $('.op').html($('textarea').val());
    return false;
});

DEMO

发布评论

评论列表(0)

  1. 暂无评论