Created
July 9, 2009 17:32
-
-
Save fguillen/143822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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