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

  • SEARCH
  • Home
  • 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 6715643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:38:19+00:00 2026-05-26T08:38:19+00:00

I have reference the answer here Select whole word with getSelection . I would

  • 0

I have reference the answer here Select whole word with getSelection. I would like to make a mouseup selection completed words from body. than wrap <p> tag for the selection words. Here is the code. So how to? Thanks.

<script src="jquery.js"></script>
<script>
(function($) {
function getSelected() {
  if(window.getSelection) { return window.getSelection(); }
  else if(document.getSelection) { return document.getSelection(); }
  else {
    var selection = document.selection && document.selection.createRange();
    if(selection.text) { return selection.text; }
    return false;
  }
  return false;
}

function expand(range) {
    if (range.collapsed) {
        return;
    }

    while (range.toString()[0].match(/\w/)) {
        range.setStart(range.startContainer, range.startOffset - 1);   
    }

    while (range.toString()[range.toString().length - 1].match(/\w/)) {
        range.setEnd(range.endContainer, range.endOffset + 1);
    }
}

$(document).ready(function() {
  $('body').mouseup(function() {
    var selectionRange = getSelected().getRangeAt(0);
    var start = selectionRange.startOffset; 
    expand(selectionRange);
    var selection = selectionRange.toString();
    if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
        //how to wrap <p> tag for the selection words?
    }
  });
});
})(jQuery);
</script>
<style>
p {color:blue; }
</style>
<body>
Facebook needs to scrape your page to know how to display it around the site.

Facebook scrapes your page every 24 hours to ensure the properties are up to date. The page is also scraped when an admin for the Open Graph page clicks the Like button and when the URL is entered into the Facebook URL Linter. Facebook observes cache headers on your URLs - it will look at "Expires" and "Cache-Control" in order of preference. However, even if you specify a longer time, Facebook will scrape your page every 24 hours.

The user agent of the scraper is: "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
</body>

UPDATE:

var new_html = $('body').html().split(selection, 1)[0] + '<p>' + selection + '</p>' + $('body').html().split(selection, 2)[1];
$('body').html(new_html);
  • 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-26T08:38:20+00:00Added an answer on May 26, 2026 at 8:38 am

    Try this:

    (function($) {
    function getSelected() {
      if(window.getSelection) { return window.getSelection(); }
      else if(document.getSelection) { return document.getSelection(); }
      else {
        var selection = document.selection && document.selection.createRange();
        if(selection.text) { return selection.text; }
        return false;
      }
      return false;
    }
    
    function expand(range) {
        if (range.collapsed) {
            return;
        }
    
        while (range.toString()[0].match(/\w/)) {
            range.setStart(range.startContainer, range.startOffset - 1);   
        }
    
        while (range.toString()[range.toString().length - 1].match(/\w/)) {
            range.setEnd(range.endContainer, range.endOffset + 1);
        }
    }
    
    $(document).ready(function() {
      $('body').mouseup(function() {
        var selectionRange = getSelected().getRangeAt(0);
        var start = selectionRange.startOffset; 
        expand(selectionRange);
        var selection = selectionRange.toString();
        if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
            var el = $('body');
            el.html(el.html().replace(selection, "<p>"+selection+"</p>"));    }
      });
    });
    })(jQuery);
    

    http://jsfiddle.net/CHhJG/

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

Sidebar

Related Questions

I have a reference to a type, for which I would like to retrieve
I frequently use Moose to make sure my data have suitable default values, like
As an extension to my previous question , I would like to automatically select
For reference here is what my web.config and code look like: <membership defaultProvider=SqlProvider userIsOnlineTimeWindow=15>
i have 3 hidden fields in 1 div. when I have reference to this
I have a reference-type variable that is readonly , because the reference never change,
I have a 'reference' SQL Server 2005 database that is used as our global
I have a reference application that I use to work through DDD issues, and
I have a reference to call that may or may not be there. When
I have a reference to a UIBarButtonItem, is there a way I can add

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.