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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:07:24+00:00 2026-05-17T23:07:24+00:00

I have a contentEditable div with following text for example: <div contentEditable=’true’> This document

  • 0

I have a contentEditable div with following text for example:

<div contentEditable='true'> This document is classified</div>

Now for example if user clicks on ‘m’ in word document I want to show a context menu containing few text choices. That context menu will be contained in a div element. I want to replace the word “document” with the option(text) selected by user from context menu. In my view I have to find absolute position of click to show context menu and then I have to find space elements before and after the caret position and then replace the selection with option selected from context menu. Any idea how I can do it using JavaScript and jQuery?
Edit:
one part of my question is about context menu and other the more important is how i can detect the word on wchich user has clicked on in contentEditable div or in text area on the other hand. my goal is something like in below picture
alt text
actually i want to make a similar transliteration application. the process of script conversion from roman to Urdu has been done however i m facing lot of problems in user interface development on the web. google transliteration application can be found here. i hope someone can help me in getting word under user’s mouse and display a context menu containing few choices.

  • 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-17T23:07:25+00:00Added an answer on May 17, 2026 at 11:07 pm

    You may want to have a look at some of the existing context menu plugins

    1. http://www.trendskitchens.co.nz/jquery/contextmenu/
    2. http://labs.abeautifulsite.net/projects/js/jquery/contextMenu/demo/
    3. http://plugins.jquery.com/plugin-tags/context-menu

    To get the currently selected word in a text area, have a look at the fieldSelection plugin.

    So using the second plugin and fieldSelection (Disclaimer: I think the indexing regarding string replacement is slightly off – have not fully tested)

    Javascript

     $(document).ready(function () {
        //replace
        $("#tx").contextMenu({
           menu: 'replace'
        }, function (action, el, pos) {
            update(action, el)
        });
    
        function update(action, el) {
            var range = $("#tx").getSelection();
            var textLength = $("#tx").val().length;
    
            var firstPart = $("#tx").val().substring(0, range.start);
            var secondPart = $("#tx").val().substring(range.start, textLength);
    
            var lastIndexofSpaceInfirstPart = firstPart.lastIndexOf(" ");
            var firstIndexofSpaceInSecondPart = secondPart.indexOf(" ");
    
            var startOfWord = 0
            var endOfWord = 0
    
    
            if (lastIndexofSpaceInfirstPart < 0) {
                // start of string
                startOfWord = 0;
            } else {
                startOfWord = lastIndexofSpaceInfirstPart;
            }
    
            if (firstIndexofSpaceInSecondPart < 0) {
                // end of textArea
                endOfWord = textLength
            } else {
                endOfWord = firstIndexofSpaceInSecondPart + range.start;
            }
    
            var word = $.trim($("#tx").val().substring(startOfWord, endOfWord));
    
            var replaceMent = $("#tx").val().substr(0, startOfWord)
                                + action.toString()
                                + $("#tx").val().substr(endOfWord, textLength);
    
                    alert(
                          "rangeStart: " + range.start +
                          "\n\nrangeEnd: " + range.end +
                          "\n\nrangeLength: " + range.length +
                        +"\n\nfirstPart: " + firstPart
                        + "\n\n secondPart: " + secondPart
                        + "\n\n lastIndexofSpaceInfirstPart: " + lastIndexofSpaceInfirstPart
                        + "\n\n firstIndexofSpaceInSecondPart:" + firstIndexofSpaceInSecondPart
                         + "\n\nWordStart: " + startOfWord
                         + "\n\nEndofWord: " + endOfWord
                         + "\n\nWord: '" + word + "'"
                         + "\n\nNew Text: '" + replaceMent + "'"
                        );
                }
            });
    

    Html

    <textarea id="tx">
        Some text
        </textarea>
    
    
        <ul id="replace" class="contextMenu">
          <li class="edit"><a href="#youwordhere1">youwordhere1</a></li>
          <li class="cut separator"><a href="#youwordhere2">youwordhere2</a></li>
          <li class="copy"><a href="#youwordhere3">youwordhere3</a></li>
          <li class="paste"><a href="#youwordhere4">youwordhere4</a></li>
          <li class="delete"><a href="#youwordhere5">youwordhere5</a></li>
          <li class="quit separator"><a href="#youwordhere6">youwordhere6</a></li>
        </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.