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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:10:40+00:00 2026-05-30T10:10:40+00:00

So I am trying to create an RTE environment. I have a content editable

  • 0

So I am trying to create an RTE environment.
I have a content editable div and I would like to allow the user to select text and then press a button which would wrap BBCode around it.

I have tried creating the following function, However, the selected text is just replaced.
It doesn’t seem to be storing the proper valu ein selectedText

function wrap(tag) 
{


    var sel, range;
    if (window.getSelection)
     {
        sel = window.getSelection();
        if (sel.rangeCount)
         {
            range = sel.getRangeAt(0);
            var selectedText = range;
            range.deleteContents();
            range.insertNode(document.createTextNode('['+tag+']'+selectedText+'[/'+tag+']'));
        }
    } 
    else if (document.selection && document.selection.createRange) 
    {
        range = document.selection.createRange();
        selectedText = document.selection.createRange().text;
        console.log(text);
        range.text = '['+tag+']'+text+'[/'+tag+']';
    }
}


</script>

JQuery is acceptable but I’d prefer regular Javascript.

  • 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-30T10:10:41+00:00Added an answer on May 30, 2026 at 10:10 am

    Change selectedText = range; to selectedText = range.toString();. The range is an object and when you do deleteContents it wipes out the data and so it doesn’t wrape.

    DEMO

    JS:

    function wrap(tag) {
        var sel, range;
        var selectedText;
        if (window.getSelection) {
            sel = window.getSelection();
    
            if (sel.rangeCount) {
                range = sel.getRangeAt(0);
                selectedText = range.toString();
                range.deleteContents();
                range.insertNode(document.createTextNode('[' + tag + ']' + selectedText + '[/' + tag + ']'));
            }
        }
        else if (document.selection && document.selection.createRange) {
            range = document.selection.createRange();
            selectedText = document.selection.createRange().text + "";
            range.text = '[' + tag + ']' + selectedText + '[/' + tag + ']';
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a select list with a first option text set to an
I'm trying create pagination on my page. The user can select the number of
I'm trying create a text area in a div with the id of "body".
Trying to create a user account in a test. But getting a Object reference
Trying to create my first iPhone app that would play back audio. When I
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a log specifically for logging user account activity. I created this
I am trying create a data.frame from which to create a graph. I have
I am trying create a layout. There are several inner layouts that should like

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.