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

The Archive Base Latest Questions

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

Is there an easy way to wrap spans around arbitrary text within an html

  • 0

Is there an easy way to wrap spans around arbitrary text within an html paragraph? For example, given the following original html:

<p>Here is a dandy block of text to color up</p>
<p> WHOAH another paragraph</p>

I’d like to wrap arbitrary portions of the text based on user input. So one set of input might transform this into

<p>Here is a <span style="background:yellow">dandy block</span> of text to color up</p>
<p> WHOAH <span style="background:green">another paragraph</span></p>

While another set of input might create

<p>Here is a<span style="background:yellow">a dandy block</span> of text to color up</p>
<p> WHOAH <span style="background:green">another</span> paragraph</p>

This problem is related to this one and this one, however, the main difference with my goal is that I want the highlights to be permanent, not just temporary selections and I’d also like this to work within p elements rather than textareas.

If it’s possible, I imagine it would look something like using jQuery

var innerText = $('p')[p_index].slice(char_start, char_end).text();
$('p')[p_index].slice(char_start, char_end).html(
    "<span style=\"background:yellow\">"+
    innerText +
    "</span>");

This would (in theory) select the p_index paragraph, grab the range between the given indices and replace it with a newly created span which has the original text nested inside of it. This clearly doesn’t work since subscripting on the jQuery object does not return another inner jQuery object. Though

$("p").slice(0, 1).html("<span style=\"background: blue\">" +
                        $("p").slice(0, 1).text() + 
                        "</span>");

Does exactly what I want on a paragraph level, but not on the within text level. I could use this approach to do the replacement by totally writing each paragraph given the character ranges I have, but if there’s an easy way, I’d greatly appreciate 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-06-10T07:04:47+00:00Added an answer on June 10, 2026 at 7:04 am
    $("p")[p_index]
    

    gives you the actual DOM element that is that paragraph at p_index, so to get the contents of the paragraph you’d need to use:

    $("p")[p_index].innerHTML
    // OR
    $("p")[p_index].textContent
    

    Using jQuery would be easier though. You wouldn’t use the jQuery slice() method to reduce the range to a single element, you’d use the .eq() method. Try something like this:

    $('p').eq(p_index).html(function(i,currentText) {
         return currentText.substring(0, char_start) +
                "<span style=\"background:yellow\">" +
                currentText.substring(char_start, char_end) +
                "</span>" +
                currentText.substring(char_end);
    });
    

    When you pass a function to the .html() method, jQuery sets the html to whatever you return from the function. jQuery passes the function the current (inner) html of the element so you can process it. (If you do this on a jQuery object containing more than one element your function is called once for each element so they can be processed individually.)

    Demo: http://jsfiddle.net/62HHk/

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

Sidebar

Related Questions

Is there an easy way to wrap a Preferences Panel thing around the Settings.settings
Is there an easy way to wrap an ADODB.Connection COM object (the one defined
Is there an easy way to make elements in tripleplay wrap and continue below?
There must be an easy way to do this, but somehow I can wrap
Is there an easy way to distribute inline elements within a parent block container
Is there an easy way to automatically wrap any h2 element in the div
I am wondering if there is an easy way to calculate the text extent
Is there an easy way to log PHP errors to a log for a
Is there an easy way to generate docs for REST api direct from a
Is there an easy way of getting the datetime of the last change to

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.