Jquery output text in htmlentities except line break

$('.click').click(function(){
    var t = 'hello /n world';
    $('.target').text(t);
});

I have a button, it replaces the line after the click.

if I use text(), it converts the entire string to htmlentites, and I cannot continue breaking the string

if i use html()i will get xss attack)

(var t should be extracted from the database from text data)

any suggestion?

+4
source share
1 answer

try using:

var t = 'hello /n world';
t = t.replace(/\/n/g, "\n")
$('.word')[0].innerText = t;

since \ n should be used for the newline character.

0
source

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


All Articles