JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 9th, 2013, 07:19 AM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
jQuery - Get text surrounding selected text

Hi guys,

Im working on an article spinner in HTML and jQuery. Whenever a user selects a word in the article textarea, you can look up synonyms for it.

To make it more user friendly, I want to show a small part of the text surrounding the selected word in a DIV, so the user will remember for what sentence he is looking for synonyms

However, I can't find a proper way to do this. So the question: how can I retrieve text surrounding a selected word in a textarea?

Is this even possible?

Im using the RangyInputs Library to manipulate selections in the textarea

Reply With Quote
  #2  
Old January 9th, 2013, 07:28 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 8 h 22 m
Reputation Power: 813
Hi,

um, haven't you looked into that library yet? There's a getSelection() method, which returns the start and end index of the selection. With that, you can select the corresponding substring of the textarea content.

Reply With Quote
  #3  
Old January 9th, 2013, 07:31 AM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
I know, but there is no clean method to get the text between two points without altering the current selection

Reply With Quote
  #4  
Old January 9th, 2013, 07:40 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 8 h 22 m
Reputation Power: 813

Reply With Quote
  #5  
Old January 9th, 2013, 07:48 AM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
How can I use that to get text outside of the current selection?
__________________
Post to Facebook using a Nokia 3310

Reply With Quote
  #6  
Old January 9th, 2013, 08:00 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 8 h 22 m
Reputation Power: 813
You have the full content of the textarea (fetched with JavaScript's native "value" method or whatever jQuery method you might be using). You have the start and end index of the selection. And you have a method to retrieve a substring between two indices.

With that, you can get any surrounding text you want. If you want the 5 characters before and after the selection, substract 5 from the start, add 5 to the end, and then use substring() with those two numbers.

Reply With Quote
  #7  
Old January 9th, 2013, 08:23 AM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
Thanks! That did it. I got this code now:

Code:
$(\"#txt\").mouseup(function() {
	$(\"#txt\").focus();
	var sel = $(\"#txt\").getSelection();
	var sentence = $(\"#txt\").val().substring(sel.start - 10,sel.end + 10);
	alert(sentence);
});
Comments on this post
Jacques1 agrees: That looks good.

Last edited by Rhytz : January 9th, 2013 at 08:28 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > jQuery - Get text surrounding selected text

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap