Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get deleted character from content editable div

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.

like image 617
Hari krishnan Avatar asked Apr 02 '13 15:04

Hari krishnan


1 Answers

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

like image 86
Denys Séguret Avatar answered Nov 14 '22 22:11

Denys Séguret