Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send text without html tags

I want to build some function in JavaScript, that sends text from textarea to a div.

I want it do the following

If the user tries to send html source to a textarea, it will show the text, and not the actual html source.

For example:

If the user tries to send: <img src='aa.png'>

I want to see in the div the text: <img src='aa.png'>, and don't want to see the actual image: aa.png

like image 243
ldoroni Avatar asked Oct 24 '14 13:10

ldoroni


1 Answers

Use .innerText or .textContent instead of .innerHTML

eleme.innerText="<img src='aa.png'>"; where eleme is your div

DEMO:

document.getElementById('test1').innerHTML="<img src='aa.png'>";
document.getElementById('test2').innerText="<img src='aa.png'>";
document.getElementById('test3').textContent="<img src='aa.png'>";
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>

You can read more for differences between this three commands and others Here

like image 89
laaposto Avatar answered Sep 28 '22 14:09

laaposto