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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:20:09+00:00 2026-05-16T01:20:09+00:00

Well, I need to replace a word, in a div contentEdible property on, by

  • 0

Well,

I need to replace a word, in a div contentEdible property on, by the same word but formatted…
Like this:
<div> My balls are big </div>

To this (replace the word: balls):
<div> My <font style="color:blue;">balls</font> are big </div>

In a contentEditable this happens dinamically, while the user type the text the replacements happens. I think that a simple event onkeydown, onkeyup, or onkey press, can solve this part.
But, the trouble is with the caret, that after all that i tryed, it stay before the word replaced, when should be stay after. I tryed write some js code, tryed find some jquery scripts, but all them failed in this situation…

Any one has some ideia, or trick ?

I think:
–> Record the length of the word unformatted.
–> Delete this word
–> Put new word formatted.
–> Walk with the caret, to position based this formatted word length.
–> Is it?
PS: I have to considerate a word in any place of this div.

I don’t know how to write this code that do what i say above.

Correct me, if i’m wrong.

Since yet, thanks!

Edit[1]: I want that this works on Mozilla Firefox, specificlly;

  • 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-16T01:20:10+00:00Added an answer on May 16, 2026 at 1:20 am

    I only have IE6/7 on this machine, but maybe you can apply the concept to other browser versions of Ranges (or maybe this is cross-browser?).

    Basically we store the cursor position, make our search/replacement, then put the cursor back where it was:

    html:

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

    and the script:

    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 = '<font style="color:blue">';
    var replaceEnd = '</font>';
    // 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(); 
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well i need some help here i don't know how to solve this problem.
I couldn't really word the title very well, but here's my problem: I've got
I'm falling deeper into the regex's dark side. I need to parse this: {{word(a|b|c)|word$1}}
I need to apply this script to work the same way for two other
I understand very well the need for websites' front ends to be coded and
I'm a beginner in ruby and in programming as well and need help with
I need to embed an entire well-formed xml document within another xml document. However,
I need the advice from someone who knows Java very well and the memory
I need a regular expression that should validate decimal point as well as range.
I need to parse a continuous stream of well-formed XML elements, to which I

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.