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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:26:46+00:00 2026-06-10T05:26:46+00:00

This is my page source : <body> <div> <p><span id=1>word</span> <span id=2>word</span> </p> <div><span

  • 0

This is my page source :

<body>
<div>
    <p><span id="1">word</span> <span id="2">word</span> </p>
    <div><span id="3">word</span> <span id="4">word</span></div>
    <ul>
        <li><span id="5">word</span> <span id="6">word</span></li>
    </ul>
    </ul>
    <p><span id="7">word</span> <span id="8">word</span> <strong><span id="9">word</span></strong> </p>
</div>
</body>

I want to highlight(apply a new class) user selected spans and get it id’s.

I can get user selected content through window.getSelection().But i don’t know how to get selected text nodes.

Thanks in advance,
Logan

  • 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-10T05:26:48+00:00Added an answer on June 10, 2026 at 5:26 am

    The window.getSelection() returns a selection object (ref) that includes the start (anchorNode) and end (extentNode). So based on the HTML you provided – with the ID’s modified to not using only numbers (ref) – here is a demo. Click on any word, or select a group of words to see them get the “red” class name.

    Modified HTML

    <div>
        <p><span id="s1">word1</span> <span id="s2">word2</span> </p>
        <div><span id="s3">word3</span> <span id="s4">word4</span></div>
        <ul>
            <li><span id="s5">word5</span> <span id="s6">word6</span></li>
        </ul>
        <p><span id="s7">word7</span> <span id="s8">word8</span> <strong><span id="s9">word9</span></strong> </p>
    </div>
    

    Script

    $('div').bind('mouseup', function(){
    
        var i,
            s = window.getSelection(),
            // get ID of starting node
            start = parseInt((s.anchorNode.parentNode.id || '').substring(1), 10),
            // get ID of end node
            end = parseInt((s.extentNode.parentNode.id || '').substring(1), 10),
            // start gathering spans
            spans = $('#s' + start);
    
        // remove selected class
        $('span[id^=s]').removeClass('red');
    
        // add each span
        for (i = start; i <= end; i++) {
            spans = spans.add( $('#s' + i) );
        }
    
        // add selected class
        spans.addClass('red');
    
    });
    

    ​
    ​

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

Sidebar

Related Questions

Im parsing the source of a website and Im using this regex: /page\.php\?id\=([0-9]*)\\>(.*)\<\/a\>\<\/span\>/.match(self.agent.page.content) self.agent.page.content
Here is my page's HTML: <body> <header></header> <div class=conteudo_representantes></div> <div class=rodape></div> </body> I have
I am creating a dialog like in this page: http://jqueryui.com/demos/dialog/#modal-confirmation (click view source) on
While viewing the source of a web page, I came across this CSS, applied
I am quite new to R. I want to compile a 1-million-word corpus of
On an iPad using Safari, go to this page: http://ifelse.org/projects/errors/viewport/test.html This is the Source:
This is the part of page source of a website. I need to grab
<body> <span id=lock>lock</span> <div id=one>test test</div> <div id=three style=display: none>hide</div> <span id=two>test</span> <div id=div_lock>
I'm trying to get the source code from this page , to get the
I have a problem with one JSF page. This is the source code: <!DOCTYPE

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.