I'm doing a chat application with WebSocket .
I used contenteditable="true"
in <div/>
, because I needed to put it like this to make it easier on other functions.
In the part of input
that sends the message to other users I have a problem, when the user types only whitespaces and clicks send, the message is sent, but I want it when it just enter blanks do not send the message.
Can anyone please help me to make this code in JavaScript or with Jquery anyway?
HTML
<div id="message" contenteditable="true">Digite sua mensagem</div>
jQuery
// ENVIA COM O ENTER
$('#message').on('keydown', function(e) {
var mymessage = $('#message').text(); //get message text
var myname = $('.myname').text(); //get user name
var mensagem = 'mensagem='+mymessage;
var to = $(this).attr('id');
var code = e.which || e.keyCode;
if (e.which == 13 && e.shiftKey == false) {
if (code == 13) {
if (mensagem != '') {
//$(".mensagens-unitarias").animate({"scrollTop": $('.mensagens-unitarias')[0].scrollHeight}, "fast");
$.ajax({
type: 'POST',
url : 'sys/teste.php',
data: 'nome='+myname+'&mensagem='+mymessage,
success: function(html){}
});
if (mymessage == "") { return false; }
//prepare json data
var msg = {
message: mymessage,
name: myname,
color : '<?php echo $colours[$user_colour]; ?>'
};
//convert and send data to server
websocket.send(JSON.stringify(msg));
}
}
return false;
}
})