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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:19:48+00:00 2026-05-19T11:19:48+00:00

I want to put some specified text (where possible/any editable field) before and after

  • 0

I want to put some specified text (where possible/any editable field) before and after any selected text in an HTML document.
document.getSelection() or document.selection.createRange().text returns only the text itself not the position.
Is there anyway to replace the selected text? Anyway to insert specific text before and after selcted text anywhere in the document?

  • 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-19T11:19:49+00:00Added an answer on May 19, 2026 at 11:19 am

    Here’s a cross-browser function to do this, which works in all major browsers and caters for the vastly different way IE does this compared to other browsers.

    Live example: http://jsfiddle.net/timdown/UWExN/64/

    function insertHtmlAtSelectionEnd(html, isBefore) {
        var sel, range, node;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
                range = window.getSelection().getRangeAt(0);
                range.collapse(isBefore);
    
                // Range.createContextualFragment() would be useful here but was
                // until recently non-standard and not supported in all browsers
                // (IE9, for one)
                var el = document.createElement("div");
                el.innerHTML = html;
                var frag = document.createDocumentFragment(), node, lastNode;
                while ( (node = el.firstChild) ) {
                    lastNode = frag.appendChild(node);
                }
                range.insertNode(frag);
            }
        } else if (document.selection && document.selection.createRange) {
            range = document.selection.createRange();
            range.collapse(isBefore);
            range.pasteHTML(html);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I want to do is put some buttons before the tabs in
For IE7 users, I want to put in some specific Please upgrade now banner.
I want to put some extra info on how program works, if theres an
I have a Zend_Form_Element_Multicheckbox and I want to put some of its elements in
I have some code that I want to put into an svn repository for
I want to put in an array some columns of a table in sql
a I have some images on page and I want to put <span> tag
I want to declare some global variables and global constants. Normally, I would put
In my application,I want to display some static files(.html,.htm,.txt) which will be uploaded by
I want to add some text to a boundfield build in the code behind

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.