Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 07:31 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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 05:59 PM. Reason: Correct typo

  • Users who have thanked Philip M for this post:

    bajjuri_6 (08-13-2010)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •