How to detect that a space was backspaced or deleted

backspace, javascript, jquery, keycode

Solution

See here: http://jsfiddle.net/Txseh/

(function(){
    var currentWhitespaceCount;

    $("input").keyup(function(e){
        var newCount = ($(this).val().match(/\s/g) || []).length;

        if (newCount < currentWhitespaceCount)
            alert("You removed one or more spaces, fool.");

        currentWhitespaceCount = newCount;
    });
})();​

It tracks the current number of whitespace characters in the input, and if ever the number goes down, it alerts(or does whatever you want).

Problem

I need to find a way to detect if a space was deleted or backspaced, and run a function if that is the case. I am working on this in JavaScript / jQuery. I know I can get the delete or backspace key press by using: ``` $(this).keyup(function(event) { event.keyCode ``` However, I do not know how to tell if the delete or backspace command removed a space? Very appreciative for any suggestions.

Original source