Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:51+00:00 2026-05-26T01:14:51+00:00

How to select multi words in window.getSelection . In this code, if I choose

  • 0

How to select multi words in window.getSelection.

In this code, if I choose tweenen paragrap, it will locked word paragraphs and missed `between. And I need select both 2 words.

Or even I choose 3 words dd 2 spa, it will select 3 words add 2 spaces. thanks.

<script type="text/javascript">
    function Selected() {
        var sel;
        if ((window.getSelection) && (sel = window.getSelection()).modify) {
            sel = window.getSelection();
            if (!sel.isCollapsed) {
                sel.modify("move", "backward", "word");
                sel.modify("extend", "forward", "word");
            }
        }
    }
</script>
<p>put returns between paragraphs, for linebreak add 2 spaces at end</p>
<input type="button" onclick="Selected();" value="selected">
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:14:52+00:00Added an answer on May 26, 2026 at 1:14 am

    If I understand you correctly, you want the button to snap the selection to whole words. If so, here’s how you can do it:

    Live demo: http://jsfiddle.net/uCHVQ/1/

    Code:

    function Selected() {
        var sel;
        if ((window.getSelection) && (sel = window.getSelection()).modify) {
            sel = window.getSelection();
            if (!sel.isCollapsed) {
    
                // Detect if selection is backwards
                var range = document.createRange();
                range.setStart(sel.anchorNode, sel.anchorOffset);
                range.setEnd(sel.focusNode, sel.focusOffset);
                var backwards = range.collapsed;
                range.detach();
    
                // modify() works on the focus of the selection
                var endNode = sel.focusNode, endOffset = sel.focusOffset;
                sel.collapse(sel.anchorNode, sel.anchorOffset);
                if (backwards) {
                    sel.modify("move", "forward", "word");
                    sel.extend(endNode, endOffset);
                    sel.modify("extend", "backward", "word");
    
                } else {
                    sel.modify("move", "backward", "word");
                    sel.extend(endNode, endOffset);
                    sel.modify("extend", "forward", "word");
                }
            }
        }
    }
    

    (By the way, is that code originally from something I’ve posted? It looks familiar but I can’t find it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
I have an .aspx page with a databound multi-select jquery dropdown. This works nicley
I've built a multi-select form (from within form_for) like this: <div class=rounded-block quarter-wide radio-group>
I have a multi-select <asp:listbox> and a button. I want to disable the button
I have a multi-select list box. I want to be able to select a
I have the following multi-select box in a HTML form, where user can select
How do you programmatically select items in a multi-select listbox using jQuery?
We currently have a form with the standard multi-select functionality of here are the
I can't seem to figure out how to correctly select a Multi Select option
Is there a way to filter a multi-line select box using jQuery? I'm a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.