Created
July 9, 2009 17:12
-
-
Save fguillen/143808 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 | |
* return the last word from cursor on a textarea | |
* example: alert( "last word from cursor: " + $('#my_textarea').lastWord() ) | |
*/ | |
jQuery.fn.lastWord = function() { | |
var buffer = ''; | |
this.each(function(){ | |
if (this.selectionStart || this.selectionStart == '0') { | |
var startPos = this.selectionStart; | |
var endPos = this.selectionEnd; | |
var scrollTop = this.scrollTop; | |
var index = 0; | |
var new_char = ''; | |
do{ | |
index += 1; | |
buffer = new_char + buffer; | |
new_char = this.value.substr(startPos - index, 1); | |
} while( new_char.search( /^(\w|\.)$/ ) != -1 ) | |
} else { | |
alert("lastWord not supported on this navigator"); | |
} | |
}); | |
return buffer; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment