my jqery always shows the error "an error occurred while receiving the message"

Asked

Viewed 24 times

0

jQuery('body').on('keyup', '.mesg', function(e){
		if (e.which == 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></div></div></div>';

<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></div></div></div>';

  • Try using this : var key = e.keycode || e.charCode; Instead of e.which;

  • still not working

  • but at least when I click on any key, this error no longer appears

1 answer

0

I tried to use this code with an INPUT type field and it worked without problem. I don’t know if div this should work ...

<input class="mesg"/>

jQuery('body').on('keyup', '.mesg', function(e){
    if (e.which == 13) {
        console.log("ENTER OK");
        //... resto do código aqui
    }else{
        console.log("NÃO É ENTER");
        //... resto do código aqui
     }
});
  • must be a div cannot be an input

  • But a div does not receive written value as I know, can’t you create an input inside the div ? Because simply with DIV, I don’t think it’s possible.

  • receives in innerHTML

  • @Pedroferreira But it only receives the value after the keystroke validation, which should be done in an input element ...

  • not if the div had the opportunity to have an Event that was specifically what she wanted

Browser other questions tagged

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