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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:00:20+00:00 2026-05-27T09:00:20+00:00

I need to select text from different paragraphs and make a span for showing

  • 0

I need to select text from different paragraphs and make a span for showing this text. See this example:

<p> this is a text </p>
<p>hello ever one </p>

Now what I want is that if I select text from the web view in my iPhone app it highlights it in a different color. For this I am making a span and setting its style. It works fine for the same paragraph but not for different paragraphs. See this:

<p> this <span class="blue">is a </span> text </p>

Class blue declares its style and it works fine, but the following does not work:

<span class="blue">
<p> this is a text </p>
<p>hello ever </span> one </p>

For solving this problem I need two spans for both paragraphs. So how can I check where the new paragraph starts? The correct HTML code is:

<span class="blue">
<p> this is a text </p></span> 
<p>   <span class="blue"> hello ever </span> one </p>

I need to get this HTML string but I get the wrong one. I have written a JavaScript function that gets the selection and makes a span according to selection. But on selecting text from two paragraphs it does not work because it gives the wrong section of HTML code. See my JavaScript code:

function highlightsText()
{
    var range = window.getSelection().getRangeAt(0);
    var selectionContents = range.extractContents();   
    var div; 
    var newDate = new Date;
    var randomnumber= newDate.getTime();
        var imageTag = document.createElement("img");
    imageTag.id=randomnumber;
    imageTag.setAttribute("src","notes.png");   
    var linkTxt = document.createElement("a");
    linkTxt.id=randomnumber;
    linkTxt.setAttribute("href","highlight:"+randomnumber);
    div = document.createElement("span");
div.style.backgroundColor = "yellow";
div.id=randomnumber;
linkTxt.appendChild(imageTag);
div.appendChild(selectionContents);
div.appendChild(linkTxt);
range.insertNode(div);
return document.body.innerHTML+"<noteseparator>"+randomnumber+"<noteseparator>"+range.toString();
}

Please provide a solution that can resolve this problem.

  • 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-27T09:00:21+00:00Added an answer on May 27, 2026 at 9:00 am

    some change in your code can work

    see this line of codes

    function highlightsText() {
    
      var range, sel;
      if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt) {
          range = sel.getRangeAt(0);
        }
        document.designMode = "on";
        if (range) {
          sel.removeAllRanges();
          sel.addRange(range);
        }
        if (!document.execCommand("HiliteColor", false, "yellow")) {
    
          document.execCommand("BackColor", false, "yellow");
        }
        document.designMode = "off";
      } else if (document.selection && document.selection.createRange) {
        range = document.selection.createRange();
        range.execCommand("BackColor", false, "yellow");
      }
    
    
      var newDate = new Date;
      var randomnumber = newDate.getTime();
      var nodeList = document.querySelectorAll(".Apple-style-span");
      for (var i = 0, length = nodeList.length; i < length; i++) {
        nodeList[i].id = randomnumber;
      }
    
      var div = document.getElementById(randomnumber);
    
      var imageTag = document.createElement("img");
      imageTag.id = randomnumber;
      imageTag.setAttribute("src", "notes.png");
    
      var linkTxt = document.createElement("a");
      linkTxt.id = randomnumber;
      linkTxt.setAttribute("href", "highlight:" + randomnumber);
    
    
      div.appendChild(linkTxt);
      range.insertNode(div);
    
      return document.body.innerHTML + "<noteseparator>" + randomnumber + "<noteseparator>" + range.toString();
    }
    

    You need make some adjustments in this code.

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

Sidebar

Related Questions

I need to prevent user from selecting text (select all or select a portion
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
I need to pull a query like this one $result = mysql_query( SELECT id,
I need a regular expression to select all the text between two outer brackets.
I need a select from table which does not have column that tells when
I need to select the position of a following sibling via XSL. This is
I'm having some trouble with the syntax on this. SELECT a.[Id] ,a.[Ext] ,a.[Title] ,b.Text
how can i express in xpath a) need select elements which contains specified keyword.
I need to select data when a page is viewed and update the 'views'
I need to select a bunch of data into a temp table to then

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.