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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:08:15+00:00 2026-05-12T17:08:15+00:00

tl;dr The idea is to allow a user to mark any text and see

  • 0

tl;dr

The idea is to allow a user to mark any text and see menu pop-up just next to the selection, with possible actions to apply to the selected text.


I need to position an absolute positioned button next to user’s selected text.

I’m binding a mouseup event to the Document, and getting the selected text, but I’m currently out of ideas on how to know where the actual selection is positioned, without wrapping it in some element, because selection of text can be across several elements, and it would mess the structure if I would wrap it.

  • 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-12T17:08:15+00:00Added an answer on May 12, 2026 at 5:08 pm

    You could position a marker span at the end of the selection, get its coordinates using jQuery, place your button at those coordinates and remove the marker span.

    The following should get you started:

    var markSelection = (function() {
        var markerTextChar = "\ufeff";
        var markerTextCharEntity = "";
    
        var markerEl, markerId = "sel_" + new Date().getTime() + "_" + Math.random().toString().substr(2);
    
        var selectionEl;
    
        return function(win) {
            win = win || window;
            var doc = win.document;
            var sel, range;
            // Branch for IE <= 8 
            if (doc.selection && doc.selection.createRange) {
                // Clone the TextRange and collapse
                range = doc.selection.createRange().duplicate();
                range.collapse(false);
    
                // Create the marker element containing a single invisible character by creating literal HTML and insert it
                range.pasteHTML('<span id="' + markerId + '" style="position: relative;">' + markerTextCharEntity + '</span>');
                markerEl = doc.getElementById(markerId);
            } else if (win.getSelection) {
                sel = win.getSelection();
                range = sel.getRangeAt(0).cloneRange();
                range.collapse(false);
    
                // Create the marker element containing a single invisible character using DOM methods and insert it
                markerEl = doc.createElement("span");
                markerEl.id = markerId;
                markerEl.appendChild( doc.createTextNode(markerTextChar) );
                range.insertNode(markerEl);
            }
    
            if (markerEl) {
                // Lazily create element to be placed next to the selection
                if (!selectionEl) {
                    selectionEl = doc.createElement("div");
                    selectionEl.style.border = "solid darkblue 1px";
                    selectionEl.style.backgroundColor = "lightgoldenrodyellow";
                    selectionEl.innerHTML = "&lt;- selection";
                    selectionEl.style.position = "absolute";
    
                    doc.body.appendChild(selectionEl);
                }
    
                // Find markerEl position http://www.quirksmode.org/js/findpos.html
            var obj = markerEl;
            var left = 0, top = 0;
            do {
                left += obj.offsetLeft;
                top += obj.offsetTop;
            } while (obj = obj.offsetParent);
    
                // Move the button into place.
                // Substitute your jQuery stuff in here
                selectionEl.style.left = left + "px";
                selectionEl.style.top = top + "px";
    
                markerEl.parentNode.removeChild(markerEl);
            }
        };
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any idea why I see in the manage applications Running tab, both my application
I'm implementing an autocomplete that will allow a user to enter in partial text
So, management has decided to implement an idea to allow the user to choose
I want to allow a user to enter a file size, using any of
The idea is to allow to peer processes to exchange messages (packets) over tcp
I am playing with an idea that would allow people to add tickets to
The idea of what I'm trying to do is allow a client to add
idea Via jQuery, I was able to mark all :first-child and :last-child elements in
Any idea on how can I track users bookmarking event? I want to find
I am using startActivityForResult() to allow the user to choose a file form the

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.