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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:12:01+00:00 2026-05-25T16:12:01+00:00

im making a mention system like the one in facebook, right now I achieved

  • 0

im making a mention system like the one in facebook, right now I achieved it with a textarea, but now I want it to work with a contenteditable div so I can format the text within.

So right now what is does is when you press the “@” and a letter it searchs in a array the results containing that letter using the JQuery-UI autocomplete, when you select the result it displays the result in the div, but now i want to highlight the mention like facebook, so I put a:

<b style="background: #somecolor"></b>

So the mention was looking like this:

<b style="background: #somecolor">Mention</b>

So far so good, but when I want to keep writing, all the new text I wrote was going inside the <b></b> looking like this:

<b style="background: #somecolor">Mention all my new text</b>

Instead of <b style="background: #somecolor">Mention</b> all my new text

So all the new text was being highlighted, here is a picture:

http://dev.frinki.com/question/1.png

So this is the javascript code im using

$("#Conversacion").bind("keydown", function(event) {
            if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }           

        }).autocomplete({
            minLength: 0,
            source: function(request, response) {
                var term = request.term,
                    results = [];
                    console.log(term);
                if (term.indexOf("@") >= 0) {
                    term = extractLast(request.term);
                    if (term.length > 0) {
                        results = $.ui.autocomplete.filter(availableTagsFollowing, term);
                    } else {
                        //results = ['Start typing...'];
                    }
                }
                response(results);
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function(event, ui) {
                var terms = split(this.innerHTML);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push('<b style="background:#E0FFC5">' + ui.item.label + '</b>');
                this.innerHTML = terms.join("");

                $('#mentionsHidden').val($('#mentionsHidden').val() + '@['+ui.item.value+':'+ui.item.label+']');
                mentionsString = $('#mentionsHidden').val();
                return false;
            }

        });

And this is the HTML Code

<div name="Descripcion" id="Conversacion" class="Conversacion" contenteditable="true"></div>

This is a picture of the Chrome’s Inspector:

http://dev.frinki.com/question/2.png

I looked in facebook to see how it’s done, this is a picture of the chrome’s inspector in facebook:

3.png (Same domain and folder of the other two images, sorry about this, this is a new user and I can’t post more than 2 links)

It’s seems that facebook when you select a person to mention creates a new line into the span that it uses so the text is separated from the <b></b>.

Well i hope that someone can help me.

By the way, I’m terrible sorry for my english, I’m from south america.

  • 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-25T16:12:02+00:00Added an answer on May 25, 2026 at 4:12 pm

    I’m just paraphrasing / summarizing the your idea: Your div is editable and the user types into it. At some condition you’re presenting a list of which the user can select. Once the selection is made, the div‘s content is modified to contain the selected value within some wrapper element. Afterwards the user types on but the characters are inserted into the wrapper instead of after the wrapper, right?

    The reason for the unwanted behavior is the focus position. Technically speaking, there is a range of selected content that gets replaced by the user’s typed characters. This range is often invisible because it spans over exactly 0 characters. It spans over some content (and possibly multiple elements) when the mouse is used to select text for example.

    Now let us assume the following HTML: <b>One</b><b>Two</>. It might be that the empty range is in between the b tags. When the next character A is entered it needs to be inserted into the document – but even though the range/caret/cursor position is known there are 3 possible destinations:

    • At the end of the first b: <b>OneA</b><b>Two</b>
    • In between the tags: <b>One</b>A<b>Two</b>
    • At the beginning of the second b: <b>One</b><b>ATwo</b>

    Your browser chooses the first approach: Roughly speaking, to keep adding to the most recently added element.

    Now, there is a pretty complex range system to specify which approach to use for placing new content. See Introduction to Range and a question on setting the range for examples.

    The general idea to solve your problem is to create some empty content after the newly inserted content. Thus ending up with <b style="background: #somecolor">Mention</b>|. Note that instead of the pipe character | you need to place an empty text node but that can’t be shown in HTML code. DOM is able to handle more than serialized HTML / HTML code! Then you would need to select the contents of the empty text node. The next character would then replace the empty text node (the range selecting the empty content within the text node would be replaced and thus the character would be inserted into the text node to be more precise). The effect is you end up with <b style="background: #somecolor">Mention</b>A assuming the entered character was A.

    In order to implement the idea I suggest to:

    • Avoid using innerHTML and use DOM methods instead. innerHTML cannot create empty text nodes.
    • Read the documentation on Document Object Model Range carefully.

    I prepared a jsFiddle to show a basic implementation. Note that this is a quick hack and supports neither ranges spanning multiple elements nor empty parents. The example wraps all upper case letters in a b tag which should be enough to get you started.

    // Get the current selection and range
    var selection = window.getSelection();
    var range = selection.getRangeAt(0);
    
    var textNode = range.startContainer;    
    var text = textNode.data;
    // Create a new node to hold the text in front of the range
    var textBeforeAddedContent = document.createTextNode(text.substr(0, range.startOffset));
    // Update current node to remove everything which goes into the just create node and the content to be replaced
    textNode.data = text.substr(range.endOffset);
    // Put stuff in front of the range into the document. Unless a non-empty selection was made the content looks now exactly as before calling this code
    textNode.parentNode.insertBefore(textBeforeAddedContent, textNode);
    
    // Place the new content in between the text nodes
    var wrapper = document.createElement('b');
    wrapper.appendChild(document.createTextNode(letter));
    textNode.parentNode.insertBefore(wrapper, textNode);
    
    // You might want to set the new range explicitly here and add an empty text node wherever newly typed letters shall go
    // Set the new selection explicitly
    range.setStart(textNode, 0);
    range.setEnd(textNode, 0);
    selection.addRange(range);
    

    Chrome fails for zero-length selections starting at offset 0 and sets a zero-length selection to end of previous child instead. An ugly hack to support Chrome could be to insert some invisible character and remove it after the next character was inserted.

    Chrome’s problems should not matter too much for the original question because it could be valid to insert a space after the wrapper element. The range’s starting offset would then be set to 1 to insert new content after the space.

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

Sidebar

Related Questions

Sorry, I feel like making someone else to do my job but I feel
I want to build a web application (SaaS) that can work both in Online
Edit: I'm sorry, but I forgot to mention that I'll need the values of
Making an app where we want to send a photo to a server along
I would like to create an expiration system for my website, where a user
Making echo of a question around the web: Is the syntax for svn:ignore patterns
When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
After making some changes in my models (eg. new field in a model and
After making a few modifications to a rails app I am tinkering on, railroad
Im making a small python script to upload files on the net. The script

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.