Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-13-2010, 03:32 PM   PM User | #1
bajjuri_6
New to the CF scene

 
Join Date: Aug 2010
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
bajjuri_6 is an unknown quantity at this point
Determining the neighbors of highlighted text

Hello All,

I am a JS newbie and I'm wondering if there a way to figure out the sentence from which a word has been highlighted.

Essentially, when a user selects a word or a phrase, I should output the sentence from which it's been selected.

Is this possible with JS or with any server side languages?

- Pallav

Last edited by bajjuri_6; 08-13-2010 at 06:31 PM..
bajjuri_6 is offline   Reply With Quote
Old 08-13-2010, 04:15 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Assuming that a sentence is defined as the text between two periods, then this will do the trick.

Code:
<html>
<head>
</head>

<body>

<div id = "myLorem">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum neque semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam lobortis neque porta a sagittis urna tristique. Suspendisse nec lacus nisi. Pellentesque fermentum massa sit amet magna hendrerit vestibulum. Sed elit libero, scelerisque eu eleifend ut, interdum gravida nunc. Etiam ut nisi sapien, et tempus sem. Nam vel mi est. Mauris congue felis ut ante bibendum vehicula. Nullam nec sapien arcu, eget cursus lorem. Donec blandit, dolor tristique ornare dictum, arcu sapien vulputate dolor, et placerat risus odio ut magna. Ut magna mauris, pellentesque at ultricies vitae, fermentum vitae dolor. 
</div>

<form name= "myform">
<textarea name = "txtarea" rows = "10" cols = "55"></textarea><br>
<input type = "button" value = "Capture Sentence Containing Highlighted Text" onclick = "getActiveText()">
<input type = "button" value = "Clear" onclick = "document.myform.txtarea.value = ''">
</form>

<script type = "text/javascript">

var x = document.getElementById("myLorem").innerHTML;
var text = "";
function getActiveText(e) { 
text = (document.all) ? document.selection.createRange().text : document.getSelection();
var firstPos = x.indexOf(text);
var secondPos = x.indexOf(".", firstPos + 1);
var newtext = x.substring(firstPos, secondPos+1);

var start = 0;
for (var i = firstPos; i > 0; i-- ){
var the_char = x.charAt(i);
if (the_char == ".") {
start = i+2;
break;
}
}

newtext = x.substring(start, firstPos) + newtext;
document.myform.txtarea.value = newtext;
return true;
}

</script>

</body>
</html>
BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

"I have not failed. I've just found 10,000 ways that won't work. " - Thomas Edison

Last edited by Philip M; 08-13-2010 at 04:59 PM.. Reason: Correct typo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
bajjuri_6 (08-13-2010)
Old 08-14-2010, 01:55 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
If you wish to capture sentences (highlighting text across a . such as adipiscing elit. Etiam ipsum) then make the following changes:-

Code:
function getActiveText(e) { 
text = (document.all) ? document.selection.createRange().text : document.getSelection();
var sellen = text.length;
var firstPos = x.indexOf(text);
var secondPos = x.indexOf(".", firstPos + 1 + sellen);
var newtext = x.substring(firstPos, secondPos+1);
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:29 AM.


Advertisement
Log in to turn off these ads.