i want to get the keyup done in a div contenteditable

Asked

Viewed 46 times

0

$('#mesg').on('keyup', function(e){
  if ( e.keyCode || e.charCode == 13) {
    var texto = $(this).innerHTML();
    var id = '$id:5';
    $.ajax({
	  type: 'POST',
	  url: 'msgamigos.php', 
	  data: { mensagem: texto, de: id },
	  success: function(retorno){
	    if(retorno == 'ok'){
		  $('.mesg').innerHTML('');
		}else{
		  alert('Ocorreu um erro ao enviar a mensagem');
		}
	  }
	});
  }else{alert('Ocorreu um erro ao receber a mensagem'); }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=\"mesg\" id=\"mesg\" tabindex=\"0\" contentEditable=\"true\" data-text=\"Publish Your news\" spellcheck=\"true\" aria-multiline=\"true\" style=\"color:grey;background:white;width:100%;height:19.5px;vertical-align:middle;border-left:1px solid grey;\"></div>';

1 answer

0

I got Keycode with var code = e.keyCode || e.which;

$('#mesg').on('keyup', function(e){
  var code = e.keyCode || e.which;
  if (code == 13) {
    // Resto do código
    alert("Enviando...");
    // Resto do código
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="mesg" id="mesg" tabindex="0" contentEditable="true" data-text="Publish Your news" spellcheck="true" aria-multiline="true" style="border: 1px solid gray; color: gray;"></div>

Browser other questions tagged

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