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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:16:29+00:00 2026-05-20T07:16:29+00:00

I have a requirement for my web app to allow the user to Print

  • 0

I have a requirement for my web app to allow the user to “Print Selected Only”. In other words, a user selects text and potentially images and then clicks this option. I’ve seen examples of getting selected text with Javascript, but haven’t found a solution for getting the selected html itself.

As an example if I have a document like so:

<html>
<head>
</head>
<body>
    <p>A bunch of text</p>
    <img src="someimage.jpg" />
    <p>Even more text</p>
</body>
</html>

If user highlights the image and the second paragraph, I’d want the javascript to return:

<img src="someimage.jpg" />
<p>Even more text</p>

Is this possible and how would one go about doing it?

Edit: I ended up going with a js library called Rangy for this.

  • 1 1 Answer
  • 2 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-20T07:16:29+00:00Added an answer on May 20, 2026 at 7:16 am

    Here is some code I found somewhere but I lost the actual link and this seems to work.

    http://jsfiddle.net/Y4BBq/

    <html lang="en">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>The serialized HTML of a selection in Mozilla and IE</title>
        <script type="text/javascript">
        function getHTMLOfSelection () {
          var range;
          if (document.selection && document.selection.createRange) {
            range = document.selection.createRange();
            return range.htmlText;
          }
          else if (window.getSelection) {
            var selection = window.getSelection();
            if (selection.rangeCount > 0) {
              range = selection.getRangeAt(0);
              var clonedSelection = range.cloneContents();
              var div = document.createElement('div');
              div.appendChild(clonedSelection);
              return div.innerHTML;
            }
            else {
              return '';
            }
          }
          else {
            return '';
          }
        }
        </script>
        </head>
        <body>
        <div>
            <p>Some text to <span class="test">test</span> the selection on.
                Kibology for <b>all</b><br />. All <i>for</i> Kibology.
        </p>
        </div>
        <form action="">
        <p>
        <input type="button" value="show HTML of selection"
               onclick="this.form.output.value = getHTMLOfSelection();">
        </p>
        <p>
        <textarea name="output" rows="5" cols="80"></textarea>
        </p>
        </form>
        </body>
        </html>
    

    enter image description here

    There are some issues with the code (I tested with safari) where it doesn’t return the exact selection.

    enter image description here
    enter image description here
    enter image description here

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

Sidebar

Related Questions

I have a requirement to implement user privilege elevation in an MVC3 web app,
I have an MVC3 C#.Net web app. I have a requirement to display a
I have a web database app that runs in IE7+ (customer requirement) It is
I have a requirement to allow users to open word document from web page
I have had a requirement for our web app (under relevant section and sub-section)which
I have a Spring-MVC, Hibernate, (Postgres 9 db) Web app. An admin user can
I have an MVC3 C# .Net web app. I am using Aspose.Words to create
We have a Java web application (app #1) running under Tomcat. Our requirement: We
I have requirement of specifying web part connections in onet.xml. So when site is
I have a reporting requirement in my web application for which I am looking

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.