Add multiple lines with jQuery using . text

Asked

Viewed 43 times

1

I am doing the project for the school, I am 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 method .append() is vulnerable because it lets you inject javascript and even html. I’ve tried the method .text(), but the messages appear on top of each other.

  • now, it was almost, but whenever I for example sent 'a' and in the message to guide a 'b' he added an 'a' to the last message

1 answer

0

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

Otherwise, it really overlaps.

Try this below. If it goes wrong, remove the parentheses from text().

$chat.append($chat.text() + '<br/>' + data.nick+' - ' + menssagem+ '<br/>' ); 
  • And the word message is with only one S, for God’s sake... look at the name of this variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.