Add multiple rows with jQuery using .text

1

I'm doing the project for school, I'm in the chat part and I have a question. In the part of sending messages in jQuery:

 socket.on('newMessage', function(data){
     $chat.append(data.nick+' - ' + menssagem+ '<br/>' ); 
 });

But the problem is that the .append() method is vulnerable, why lets inject javascript and even html. I already tried the .text() method, but the messages appear one on top of the other.

    
asked by anonymous 06.04.2016 / 19:43

1 answer

0

You have to add the new message after content that already exists.

Otherwise, it even overlaps.

Try this below. If it goes wrong, it takes the parentheses out of text() .

$chat.append($chat.text() + '<br/>' + data.nick+' - ' + menssagem+ '<br/>' ); 
    
06.04.2016 / 19:49