Send text without html tags

I want to create some function in JavaScript that sends text from textarea to div .

I want him to do the following

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

For instance:

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

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

+5
source share
1 answer

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 learn more about the differences between the three teams and others here.

+10
source

Source: https://habr.com/ru/post/1205439/


All Articles