Thinking on hiring me?

Please read

Fernando Guillén

a Freelance Web Developer

cabecera decorativa

software development as an artistic expression

jQuery: returning the last word from textarea’s cursor

I was enjoying trying to develop a kind of completor suggestor on a textarea, I wanted it to jump when the tab key was pressed, that what easy with capturing the keydown event and the help of event.preventDefault.

But the thing what wasn’t such easy was the capturing the last word on the textarea from the cursor.

But with the help of the code of the insertAtCaret function I was abble to obtain it:

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;
};

Example of use:

alert( "last word from cursor: " + $('#my_textarea').lastWord() );

Don’t copy and paste from here, WP does weird stuff with code, download from here: http://gist.github.com/143808

This script is almost not tested, it works for me on FireFox 3.0.11 and Safari 4.0.1, so use it under your own responsability.

12 Comments to “jQuery: returning the last word from textarea’s cursor”
  1. Fernando Guillen, un Desarrollador Web Freelance » Blog Archive » jQuery: deleting the last word from textarea’s cursor Says:

    […] nada por aquí y nada por allá ó el desarrollo de software como expresión artística. « jQuery: returning the last word from textarea’s cursor […]

  2. online stock trading guru Says:

    Hey, great blog…but I don’t understand how to add your site in my rss reader. Can you Help me, please :)

    I’m Out! :)

  3. chistes verdes Says:

    chistes verdes…

    […]Fernando Guillen, a Freelance Web Developer » Blog Archive » jQuery: returning the last word from textarea’s cursor[…]…

  4. http://orderflowers1800.webs.com Says:

    Nice blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my blog stand out.
    Please let me know where you got your design.

    Thank you

  5. garden tool storage hooks Says:

    You ought to take part in a contest for one of the finest blogs online.
    I am going to highly recommend this web site!

  6. google Says:

    Hello there, just became mindful of the blog page as a result of The search engines, observed that it’s really useful. Now i’m likely to watch out for the city. My business is thankful if you proceed this specific in future. Many people will likely be had good results out of your producing. Kind regards!

  7. car shipping usa Says:

    Hi there to every one, it’s in fact a pleasant for me to pay a visit this
    website, it includes useful Information.

  8. www.Rafalromaniuk.pl Says:

    y8 dua xe
    y8 dua xe là một trò chơi vui , mà trong đó những
    con virus thường nhật con người tưởng chừng không thấy được
    bằng mắt. Bây chừ được mô phỏng thành những hình vẽ
    đáng yêu. Đặc biệt là những con virus sẽ cùng nhau sở hữu những
    chiếc xe hơi , và tham dự cuộc đua xem ai sẽ là người về
    đích.

    Nhiệm vụ của người chơi trong trò chơi y8 dua xe này
    đó Ấy là chọn cho mình một con virus. Sau thời gian ấy điều khiển virus thực hiện những
    cuộc đua. Cho rằng chỉ là những chú virus bé nhỏ , nhưng sức hấp
    dẫn của trò chơi cũng không khác với nhiều trò chơi đua xe khác.
    Hãy cùng chơi game để cảm nhận những điều thú vị
    trong đó nhé.

    Cách chơi game y8 dua xe
    đầu tiên bạn hãy chọn kiểu xe cho mình.

    Có 3 mẫu cho bạn tuyển trạch đó là: Wij’s Toastarossa
    , Wanda’s Wizzbang , Wez’s Camp. Mỗi kiểu xe sẽ nhịp độ dưới mức bình thường và độ cua khác nhau.
    Hãy xem kỹ để chọn chiếc xe mà mình yêu thích chậm chạp tối ưu nhé.

    Tiếp đến chúng tôi sẽ chọn địa hình đường đua là trên mây hay trong rừng rậm.
    Ví như trong y8 dua xe rừng rậm địa hình sẽ khó khắn hơn ,
    vì có nhiều vòng cua , trường đường lắm
    dốc và nhiều quãng đường vòng quanh. Sẽ rất khó có thể chiến thắng nếu bạn không biết cách chọn kiểu xe hợp lý.
    Hãy cân nhắc kỹ trước khi chọn mẫu xe cho các địa hình bạn sẽ đua nhé.

    Sử dụng phím mũi tên để điều khiển hướng
    mũi di chuyển của xe. Lưu ý rằng trên đường đua có rất nhiều
    đồng tiền vàng được sắp xếp sẵn , hãy cố gắng thu thập thật nhiều
    nhé. Vì thu thập càng nhiều bạn sẽ có số điểm càng lớn.

  9. daniel Says:

    Fastidious response in return of this question with solid arguments and telling the whole thing concerning that.

  10. test Says:

    Can I just say what a comfort to discover someone that truly understands what
    they’re discussing on the internet. You definitely realize how to bring a
    problem to light and make it important. More people ought to check this out and understand this
    side of your story. I can’t believe you are not more popular given that you most certainly have the gift.

  11. LoyceDPuente Says:

    Its just like you learn my mind! You appear to know a lot approximately this, just like you
    wrote the e book inside it or something. I think which you could do
    with several p.c. to pressure the message house a bit, but aside
    from that, this is wonderful blog. A great read. I’ll definitely be back.

    Feel free to surf to my website: LoyceDPuente

  12. https://www.youtube.com/watch?v=3lljIO97BGI Says:

    I got thiѕ web paցe from my friend who informed mе on the topic ⲟf this web site and now thіѕ
    time I am visiting thіѕ website ɑnd reading vеry informative posts аt tɦiѕ
    plɑcе.

Leave a comment

You must be logged in to post a comment.

a Freelance Web Developer is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

Creative Commons License
Fernando Guillen's blog by Fernando Guillen is licensed under a Creative Commons Attribution-NoDerivs 3.0 Unported License.