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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:15:54+00:00 2026-05-16T04:15:54+00:00

This is the code (now is full): HTML: <div id=content contentEditable=true onkeyup=highlight(this)>This is some

  • 0

This is the code (now is full):

HTML:


<div id="content" contentEditable="true" onkeyup="highlight(this)">This is some area to type.</div>

Javascript:


function highlight(elem){
    // store cursor position
    var cursorPos=document.selection.createRange().duplicate();
    var clickx = cursorPos.getBoundingClientRect().left; 
    var clicky = cursorPos.getBoundingClientRect().top; 
    // copy contents of div
    var content = elem.innerHTML;
    var replaceStart = '';
    var replaceEnd = '';
    // only replace/move cursor if any matches
    // note the spacebands - this prevents duplicates
    if(content.match(/ test /)) {
        elem.innerHTML = content.replace(/ test /g,' '+replaceStart+'test'+replaceEnd+' ');
        // reset cursor and focus
        cursorPos = document.body.createTextRange();
        cursorPos.moveToPoint(clickx, clicky);
        cursorPos.select(); 
    }   
}

Just woks on IE, unhapply.
Anyone can ‘adjust’ this code, to work on FF too!…

Thanks

Edit[1]:
Div Editable and More… More

This code replaces a especific word by the same word formatted…
And the caret (cursor) stay always after the word replaced! <<< “This is the big”
But just works on IE, and I like so much to rewrite this code to work on FF… but I can’t do it… Its so hard…

Anyone can help?


Edit[2]:
My problem is just with this part:


        // reset cursor and focus
        cursorPos = document.body.createTextRange();
        cursorPos.moveToPoint(clickx, clicky);
        cursorPos.select(); 

Because, moveToPotion and select functions just works on IE… Until then it is easy…
On FF there is another set of functions that make it possible… But i don’t know how to write another code that do the same things. Do you got it?

  • 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-16T04:15:54+00:00Added an answer on May 16, 2026 at 4:15 am

    You can preserve the caret position by inserting a marker element at its current location before doing your replacement on the element’s innerHTML. (Using DOM methods to traverse the text nodes and searching each for the text you want would be preferable to using innerHTML, by the way).

    The following works, so long as the caret is not positioned within or adjacent to the word “text”. I also added a timer to prevent calling this function every time a key is pressed and to wait for the user to stop typing for half a second.

    function insertCaretMarker() {
        var range;
        var markerId = "sel_" + new Date() + "_" + ("" + Math.random()).substr(2);
        if (window.getSelection) {
            var sel = window.getSelection();
            range = sel.getRangeAt(0);
            range.collapse(true);
            var markerEl = document.createElement("span");
            markerEl.appendChild(document.createTextNode("\u00a0"));
            markerEl.id = markerId;
            range.insertNode(markerEl);
        } else if (document.selection && document.selection.createRange) {
            range = document.selection.createRange();
            range.collapse(true);
            if (range.pasteHTML) {
                range.pasteHTML("<span id=\"" + markerId + "\">&nbsp;</span>");
            }
        }
        return markerId;
    }
    
    function restoreCaret(markerId) {
        var el = document.getElementById(markerId);
        var range;
        if (el) {
            if (window.getSelection && document.createRange) {
                var sel = window.getSelection();
                range = document.createRange();
                range.setStartBefore(el);
                sel.removeAllRanges();
                sel.addRange(range);
            } else if (document.body.createTextRange) {
                range = document.body.createTextRange();
                range.moveToElementText(el);
                range.collapse(true);
                range.select();
            }
            el.parentNode.removeChild(el);
        }
    }
    
    function preserveCaretPosition(func) {
        var id = insertCaretMarker();
        func();
        restoreCaret(id);
    }
    
    var highlightTimer;
    
    function highlight(elem) {
        if (highlightTimer) {
            window.clearTimeout(highlightTimer);
        }
        highlightTimer = window.setTimeout(function() {
            highlightTimer = null;
            var replaceStart = '<b>';
            var replaceEnd = '</b>';
            // only replace/move cursor if any matches
            // note the spacebands - this prevents duplicates
            if (elem.innerHTML.match(/ test /)) {
                preserveCaretPosition(function() {
                    elem.innerHTML = elem.innerHTML.replace(/ test /g, ' ' + replaceStart + 'test' + replaceEnd + ' ');
                });
            }
        }, 500);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you must do it without ajax, the following may… May 16, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer It returns false because your dateTime value has a time… May 16, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer If you write your scenarios with a focus on the… May 16, 2026 at 10:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a website coded with XML+XSLT that outputs me a full HTML website.
Below is my full code, its basic, you select a country and it shows,
I have this code: <ComboBox Width=100 ItemsSource={Binding FontList} x:Name=fontComboFast> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate>
Now, before you jump at how you shouldn't mix scopes: I realize this. However,
Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I feel like this is a simple question, but I am still relatively new
I am using the debugger to step through my code. The code file I’m
Ok, so I swear this question should be all over the place, but its
I'm developing an app that uses Core Data for save and retrieve data. Now

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.