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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:46:37+00:00 2026-05-23T17:46:37+00:00

I have a simple javascript question in the HTML page. In my case, the

  • 0

I have a simple javascript question in the HTML page.

In my case, the user can use the mouse to select some text in the paragrahe, e.g. use the mouse to highlight the ‘My name is John’, and then click the one of the input button, then the selected text will be applied to selected button style to the text (e.g. add the CSS colour and clear the CSS colour).

For example, if I use the mouse to highlight the ‘My name is John’, and click ‘red’ button, the text of ‘My name is John’ will become red.

There is 1 HTML page and 1 js file.

I have the following code in the HTML page:

// text.html
// 3 input buttons with red, yellow and green colour
// 1 input button to clear the selected CSS colour
// 1 input button to clear all the selected CSS colour
<p>
<input type="button" onclick="colour('red')" value='red'/>
<input type="button" onclick="colour('yellow')" value='yellow'/>
<input type="button" onclick="colour('green')" value='green'/>
<input type="button" onclick="clear_colour()" value='Glear'/>
<input type="button" onclick="clear_colour_all()" value='Clear All'/>
</p>

// paragrahe
<p>
My name is John, 
I live in ABC and I have a car. 
I like to play TV game.
<p>

The text.js file

//text.js
function colour(colour) {
var selection= window.getSelection().getRangeAt(0);
        var selectedText = selection.extractContents();
        if (selectedText != '') {
                        // I don't know how to do in here!!!!!!!
        }
}

function clear_colour() {
    var selection= window.getSelection().getRangeAt(0);
            var selectedText = selection.extractContents();
            if (selectedText != '') {
                            // I don't know how to do in here!!!!!!!
                            // I just find the following code
                            // var selection= window.getSelection().getRangeAt(0);
                //var selectedText = selection.extractContents();
            }
}

function clear_colour_all() {
    var selection= window.getSelection().getRangeAt(0);
            var selectedText = selection.extractContents();
            if (selectedText != '') {
                            // I don't know how to do in here!!!!!!!
            }
}

And I expect the result like after select the text and select ‘red’ button (assum I have the CSS class is red)

// paragrahe
<p>
<span class="red">My name is John, </span>
I live in ABC and I have a car. 
I like to play TV game.
<p>

if the user select the ‘My name is John’ and click the ‘clear’ button, it will return to

// paragrahe
<p>
My name is John,
I live in ABC and I have a car. 
I like to play TV game.
<p>

P.S Due to some reasons, I don’t want to use the jQuery or some other library to do so, but it is welcome to give the answer to use the jQuery. Thank you very much.

  • 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-23T17:46:38+00:00Added an answer on May 23, 2026 at 5:46 pm

    This will do the trick to wrap the selected text with a <span> node. I think you will need to change the getSelection() method in order to make it work in IE, but it works in FF:

    function colour(colour) {
      var selection= window.getSelection();
      if (selection.toString()!=="") {
        var range = selection.getRangeAt(0);
        var span = document.createElement("span");
        span.className = colour;
        span.innerHTML = range.toString();
        range.deleteContents();
        range.insertNode(span);
      }
    }
    

    EDIT: The other two functions:

    function clear_colour() {
      var selection=  window.getSelection();
      if (selection.toString()!=="" && 
          selection.anchorNode.parentNode.nodeName==="SPAN") {
        selection.anchorNode.parentNode.className="";
     }
    }
    
    function clear_colour_all() {
      var selection= document.getElementById('content');
      var spans = selection.getElementsByTagName("span");
      for(var i=0;i<spans.length;i++){
      spans[i].className="";
      }
    }
    

    It have some weird behavior depending on how you select the text, but now you have something to work with 😉

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

Sidebar

Related Questions

I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
Quick question, I have a simple javascript problem that I don't know the fix
I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.
I'm working on a simple javascript login for a site, and have come up
I have simple regex \.*\ for me its says select everything between and ,
let's say I have a simple javascript like so: $(document).ready(function(){ if(login == 1) {
I have a question that is probably very simple, but I cannot find an
Simple question. I have an XML file with a few dozens of comment blocks.
New to Javascript and jQuery, so I assume this is a pretty simple question.
Ok, i have simple scenario: have two pages: login and welcome pages. im using

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.