Get deleted character from content editable div

character, html, javascript, jquery

Solution

I don't have a clean solution. But this one shows you the removed char if you hit the backspace key :

var lastText = $('#editable').text();
$('#editable').keyup(function(event){
  if (event.which==8 || event.which==46) {
     var newText =  $(this).text();
    for (var i=0; i<lastText.length-1; i++) {
      if (lastText[i]!=newText[i]) {
       console.log("char '" +  lastText[i] + "' was removed at index "+i);
        lastText = newText;
        return;
      }
    }
  }
});

Demonstration

Problem

I am using a `content editable div` because i want to show emoticons selected by the user in the text area. I want to know what `character` is `deleted` with `backspace` or `delete` button. Is it possible to know the character with `jquery` or `javascript`? UPDATE This is not at all a duplicate, since all answer are about how to track a pressed key not about the delected character.

Original source

Related problems