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 8935345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:58:06+00:00 2026-06-15T09:58:06+00:00

The content management system I wrote for our site uses a bookmarklet to post

  • 0

The content management system I wrote for our site uses a bookmarklet to post articles, which reads the selected area on a page with document.getSelection(). But in some cases it would be very useful to read the underlying HTML code for the selected area as well, to get links and other HTML formatting.

Anyone know of a jQuery plugin or other Javascript technique to access the raw HTML that produces a selected area?

  • 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-06-15T09:58:06+00:00Added an answer on June 15, 2026 at 9:58 am

    First, as you said, get the selection

    var sel = document.getSelection();
    

    This does have some details about selected nodes, too, but if you want to do more then convert this to a range (If .rangeCount > 1 you may want to loop here)

    var range = sel.getRangeAt(0);
    

    Next, using range.commonAncestorContainer and range.startContainer walk through the DOM tree performing whatever you want until you reach range.endContainer.
    All of these nodes should be in the selection.


    Here is some code that’ll return all (top level) selected nodes and, optionally, apply a callback to every node in selection.

    function selectedNodes(callback, context) {
        var sel = document.getSelection(),
            range = sel.getRangeAt(0),
            indices = [],
            nextNode = function nextNode(e) {
                if (e.childNodes.length > 0) return e.childNodes[0];
                while(!e.nextSibling && e.parentNode) e = e.parentNode;
                return e.nextSibling;
            },
            e = range.startContainer;
        if (callback) {
            callback.call(context, e);
            while(e !== range.endContainer) {
                e = nextNode(e);
                callback.call(context, e);
            }
            e = range.startContainer;
        }
        if (e === range.commonAncestorContainer) return [e];
        else {
            while (e !== range.commonAncestorContainer) {
                indices[0] = Array.prototype.indexOf.call(e.parentNode.childNodes, e);
                e = e.parentNode;
            }
            e = range.endContainer;
            while (e !== range.commonAncestorContainer) {
                indices[1] = Array.prototype.indexOf.call(e.parentNode.childNodes, e);
                e = e.parentNode;
            }
            return Array.prototype.slice.call(e.childNodes, indices[0], indices[1]+1);
        }
    }
    
    /*
    selectedNodes(console.log, console);
    node1
    ..
    nodeN
    [node1, .., nodeM] // only top-level
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site which is using DNN (DotNetNuke) as a content management system.
In a content management system, moderators have to approve changes to existing articles. Currently
I have content management system application that uses a polymorphic tree table as the
Our project is a content management system supporting several dozen of our websites. The
We're launching a new content management system and redoing our user input forms in
I have a content management system in which a user can dynamically create an
I am writing a site with fuel_cms(content management system) . My problem is that
I wrote a basic content-management system for my website, including an administration panel. I
I've written a content management system that uses a server-side regular expression to escape
I have a content management system (Omeka) which is built on the Zend framework.

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.