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е.