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.
Julio 9th, 2009 at 7:33 pm
[…] 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 […]
Enero 11th, 2010 at 4:00 am
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! :)
Septiembre 15th, 2012 at 3:37 pm
chistes verdes…
[…]Fernando Guillen, a Freelance Web Developer » Blog Archive » jQuery: returning the last word from textarea’s cursor[…]…
Julio 3rd, 2013 at 8:11 am
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
Marzo 4th, 2014 at 5:52 pm
You ought to take part in a contest for one of the finest blogs online.
I am going to highly recommend this web site!
Marzo 9th, 2014 at 6:25 am
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!
Junio 5th, 2014 at 7:38 am
Hi there to every one, it’s in fact a pleasant for me to pay a visit this
website, it includes useful Information.
Septiembre 16th, 2015 at 4:26 am
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.
Diciembre 13th, 2015 at 8:02 pm
Fastidious response in return of this question with solid arguments and telling the whole thing concerning that.
Marzo 13th, 2016 at 9:46 am
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.
Septiembre 30th, 2016 at 10:35 pm
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
Mayo 19th, 2018 at 3:27 am
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е.