Hey.
I have a WebSoket chat, and I need to complete this task. When sending my message to the server, the div class must be set to mymessage, otherwise message. How can I accomplish this task on JS? Thank you in advance.
Hey.
I have a WebSoket chat, and I need to complete this task. When sending my message to the server, the div class must be set to mymessage, otherwise message. How can I accomplish this task on JS? Thank you in advance.
Each message has its own id. In the place where the list of messages is generated, each div element must be assigned id = id_message.
Then, by sending a message, a new message is generated with a new id.
This id is passed to the JavaScript function, which will be the following code for changing the class:
$('#id_message').attr('class', 'mymessage'); And in idel instead of attr it is better to use .removeClass and .addClass .
P.S. I would send a message to implement asynchronous-ajax, which in return will return to us json, which will contain the message id. And after that you change the class to mymessage.
I do not fully understand the data sent and received?
var ws = new WebSocket('ws://site'); //при получении ws.onmessage = function(e) { $('.class_div').html(e.data); }; //при отправки WSSend = function(msg,ws) { ws.send(msg); $('.class_div').html(msg); } WSSend('тра-ля-ля',ws); Source: https://ru.stackoverflow.com/questions/380476/
All Articles