Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save puterleat/705522 to your computer and use it in GitHub Desktop.

Select an option

Save puterleat/705522 to your computer and use it in GitHub Desktop.

Revisions

  1. puterleat revised this gist Nov 18, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jquery_textarea_last_word.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    * fguillen: 2009-07-09
    * return the last word from cursor on a textarea
    * example: alert( "last word from cursor: " + $('#my_textarea').lastWord() )
    * fixed bug which caused runaway loop when pattern doesn't recognise the start of the textarea (bigfudge)
    */
    jQuery.fn.lastWord = function() {
    var buffer = '';
  2. puterleat revised this gist Nov 18, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery_textarea_last_word.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ jQuery.fn.lastWord = function() {
    index += 1;
    buffer = new_char + buffer;
    new_char = this.value.substr(startPos - index, 1);
    } while( new_char.search( /^(\w|\.)$/ ) != -1 )
    } while( (new_char.search( /^(\w|\.)$/ ) != -1) & (startPos - index > -1 ) )

    } else {
    alert("lastWord not supported on this navigator");
  3. @fguillen fguillen revised this gist Jul 9, 2009. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jquery_textarea_last_word.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    /*
    * 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 = '';
  4. @fguillen fguillen created this gist Jul 9, 2009.
    29 changes: 29 additions & 0 deletions jquery_textarea_last_word.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /*
    * fguillen: 2009-07-09
    * return the last word from cursor on a textarea
    */
    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;
    };