Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created July 9, 2009 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fguillen/143822 to your computer and use it in GitHub Desktop.
Save fguillen/143822 to your computer and use it in GitHub Desktop.
/*
* fguillen: 2009-07-09
* delete the last word from cursor on a textarea
* example: $('#my_textarea').deleteLastWord();
*/
jQuery.fn.deleteLastWord = function() {
this.each(function(){
if (this.selectionStart || this.selectionStart == '0') {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
var lastWord = $(this).lastWord();
startPos = startPos - lastWord.length;
this.value =
this.value.substring(0, startPos) +
this.value.substring(endPos, this.value.length);
this.focus();
this.selectionStart = startPos;
this.selectionEnd = startPos;
this.scrollTop = scrollTop;
} else {
alert("deleteLastWord not supported on this navigator");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment