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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:22:00+00:00 2026-05-19T03:22:00+00:00

I am trying to learn how to write a bookmarklet where I can highlight

  • 0

I am trying to learn how to write a bookmarklet where I can highlight some text, click on the bookmarklet and have it tell me what got highlighted. I can get that far, but next I want to know what element that text is in.

For example:

<div id="some-id">to be highlighted</div>

The bookmarklet code:

javascript:(function(){alert(window.getSelection();})()

If I highlight the text “to be highlighted” and then click on the bookmarklet, it will alert the text. But how can I get the element in which the text is in, in this case the element after that?

So the flow is: highlight text, click bookmarklet, bookmarklet tells you what you highlighted and the element it’s in.

Thanks!

  • 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-19T03:22:01+00:00Added an answer on May 19, 2026 at 3:22 am

    Try something similar to this to get the dom element that contains the selected text.

    window.getSelection().anchorNode.parentNode
    

    It works on firefox and Chrome, you should test it into the remaining browsers.

    It have a quirk, if you select text that beholds to more than an element, only the first one is returned. But maybe you can live with this.

    Just for reference on what is the anchorNode property:
    http://help.dottoro.com/ljkstboe.php

    On internet explorer this snippet should do the trick (I can’t test it)

    document.selection.createRange().parentElement();
    

    as stated into
    http://msdn.microsoft.com/en-us/library/ms535872.aspx and
    http://msdn.microsoft.com/en-us/library/ms536654.aspx

    A range explanation on quirksmode: http://www.quirksmode.org/dom/range_intro.html

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

Sidebar

Related Questions

I'm currently trying to learn OO Javascript so I can write some cleaner code.
Greetings, everyone. I'm trying to learn some Silverlight basics, and have decided to write
OK, so I'm trying to learn JavaScript properly so that I can write good,
I am trying to learn how to write plugins using SIMBL. I got my
I am trying to learn Ruby on Rails and trying to write some of
I'm trying to learn Scala and tried to write a sequence comprehension that extracts
So I'm trying to learn SQLCLR and have chosen to write a table-valued function
I have been trying to learn how to write faster more efficient jQuery and
I am trying to learn how to write more dynamic web sites that use
I'm trying to learn python, so I decided to write a script that could

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.