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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:39:48+00:00 2026-05-30T03:39:48+00:00

I’m searching for cross browser solution (Chrome, FF, Opera > 10, IE >= 8)

  • 0

I’m searching for cross browser solution (Chrome, FF, Opera > 10, IE >= 8) for the following problem:

There is some html code:

<div>
    <div id="one">
        <p id="red">red</p>
        <p id="green">green</p>
    </div>
    <div id="two">
        <p id="blue">blue</p>
        <p id="black">black</p>
    </div>
</div>

and user selects with mouse text from ‘een’ (in #green node) to ‘blu’ (in #blue node). How can I check if #blue is inside selection (it does not matter if is whole or partially selected) and #red and #black are not in selection.
Simple API would look like this:

Selection.isElementSelected(document.getElementById('black'));

I’ve tried to use DOMSelection and Ranges but the problem is that I need to check elements in nested structure. In Chrome I can use Range.intersectsNode(), but this is the only browser with support of this method.

Any suggestions?

  • 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-30T03:39:50+00:00Added an answer on May 30, 2026 at 3:39 am

    After some research I’ve made solution based on following materials:
    https://developer.mozilla.org/en/DOM/range

    http://msdn.microsoft.com/en-us/library/ms535872(v=vs.85).aspx

    http://www.quirksmode.org/dom/range_intro.html

    In case of IE8 entire node must be selected but this is acceptable for me.

    Here is is some playground on jsfiddle: http://jsfiddle.net/58Uvd/
    And here is code:

    (function () {
    
        function UserRange (win) {
            this.win = win || window;
            this.doc = this.win.document;
    
            this.userRange = this.createUserRange();
        }
    
        var NonIEPrototype = {
    
            constructor: UserRange,
    
            createUserRange: function () {
                var selection = this.win.getSelection();
                return selection.rangeCount
                    ? selection.getRangeAt(0)
                    : this.doc.createRange();
            },
    
            overlapsNode: function (node) {
                return this.intersectsNode(node);
            },
    
    
            intersectsNode: function (node) {
                // this method is implemented in Firefox with Gecko before 1.9
                // and other non-IE browsers
                if (this.userRange.intersectsNode) {
                    return this.userRange.intersectsNode(node);
                }
    
                return this.intersectsRange(this.createRangeWithNode(node));
            },
    
            createRangeWithNode: function (node) {
                var rangeWithNode = node.ownerDocument.createRange();
                try {
                    rangeWithNode.selectNode(node);
                }
                catch (ex) {
                    rangeWithNode.selectNodeContents(node);
                }
                return rangeWithNode;
            },
    
            intersectsRange: function (range) {
                return this.userRange.compareBoundaryPoints(Range.END_TO_START, range) === -1 &&
                    this.userRange.compareBoundaryPoints(Range.START_TO_END, range) === 1;
            }
        };
    
        var IEPrototype = {
    
            constructor: UserRange,
    
            createUserRange: function () {
                return this.doc.selection.createRange();
            },
    
            overlapsNode: function (node) {
                var rangeWithNode = this.createRangeWithNode(node);
                return this.containsRange(rangeWithNode);
            },
    
            createRangeWithNode: function (node) {
                var range = node.ownerDocument.selection.createRange();
                range.moveToElementText(node);
                return range;
            },
    
            containsRange: function (range) {
                return this.userRange.inRange(range);
            }
        };
    
        UserRange.prototype = window.getSelection ? NonIEPrototype : IEPrototype;
        window.UserRange = UserRange;
    
    }());
    

    And usage:

    var userRange = new UserRange();
    userRange.overlapsNode(document.getElementById('node-to-check'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.