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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:02:39+00:00 2026-05-17T15:02:39+00:00

I am writing a script on which the user needs to be able to

  • 0

I am writing a script on which the user needs to be able to select some text which is sent via ajax to the backend script for further process.

I can select plain text nodes fine or text nodes that have bold, italic or underlined text inside it.

For e.g

<p>This is <strong>some</strong> cool <em>italic</em> text, <u>really!</u></p>

So, that works, that is cool.

However, the issue is, if the text node starts with hsome bold, italic or underlined text OR even headings it outputs the following error on firefox console:

The boundary-points of a range does not meet specific requirements." code: "1 range.surroundContents($('<span...wAnno_'+newLen+'"></span>').get(0));

The error is output when the user selects something like:

<strong>Mark says</strong> Hi

OR

<em>Mark says</em> Hi

OR

<u>Mark says</u> Hi

The same error outputs even if a text is enclosed inside heading tags e.g <h2>test</h2>

My code looks like:

var select = window.getSelection();
var parents = $(select.focusNode).parents('.the-content');

if($(select.focusNode).parent().hasClass('.highlighted')) {
    alert('This text is already highlighted');
} else {
    for(var i = 0; i < select.rangeCount; i++) {
        var range = select.getRangeAt(i); 
        range.surroundContents($('<span class="newHighlight" id="newHigh_'+newLen+'"></span>').get(0));
    }
}

var selectedText = select.toString();

I need help with fixing this.

Help with the code will be awesome.

  • 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-17T15:02:39+00:00Added an answer on May 17, 2026 at 3:02 pm

    The problem is that the surroundContents method of Range can’t work on a Range where the start and end boundaries lie within different elements, because surrounding the contents of such a Range within an element would not produce valid HTML. If changing the background colour of your Range is all you need to do, you could use the following trick with document.execCommand:

    function highlight(colour) {
        var range, sel;
        if (window.getSelection) {
            // Non-IE case
            sel = window.getSelection();
            if (sel.getRangeAt) {
                range = sel.getRangeAt(0);
            }
            document.designMode = "on";
            if (range) {
                sel.removeAllRanges();
                sel.addRange(range);
            }
            // Use HiliteColor since some browsers apply BackColor to the whole block
            if ( !document.execCommand("HiliteColor", false, colour) ) {
                document.execCommand("BackColor", false, colour);
            }
            document.designMode = "off";
        } else if (document.selection && document.selection.createRange) {
            // IE case
            range = document.selection.createRange();
            range.execCommand("BackColor", false, colour);
        }
    }
    

    Otherwise, you’ll need to walk through the text nodes within the range and surround each with a <span>, which is not trivial. I’ve been working on a cross-browser range and selection library that includes a module for applying a CSS class to the contents of a selection or Range at http://code.google.com/p/rangy/, although that module is a few days away from being documented and released.

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

Sidebar

Related Questions

I'm writing a script which manages a very large table. When a user clicks
I am writing a script which is likely to be modified by users. Currently
I am writing a batch script which I wish to open a file and
I'm writing a script in python which basically queries WMI and updates the information
For writing scripts for process automation in Linux platform, which scripting language will be
I'm writing a script to remove some build artifacts older than 1 week. The
I'm writing a script that has to move some file around, but unfortunately it
I am working on writing some code to scrub user input to my ASP.NET
I am writing a script in php, which is quite similar to a shopping
I am writing a VB Script that uses WMI to determine which adapter is

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.