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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:08:10+00:00 2026-05-22T17:08:10+00:00

Just for fun, I want to build simple text formatting tools for a textarea.

  • 0

Just for fun, I want to build simple text formatting tools for a textarea. To begin, I want to be able to enclose high-lighted text in a textarea with ** if it is to be in bold format (just like stackoverflow’s textarea editor). I’ve written the following code, which works most of the time, but it does have a bug, which I’ll explain shortly.

<html>
<head>
<script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script type="text/javascript" language="javascript">
var tmpText = '';
$(document).ready(function(){
        tmpText = '';
        $('#btn_bold').click(function(){bold(tmpText);});
        $('textarea').bind('mouseup', function(){
                  tmpText = '';
                  if(window.getSelection){
                    tmpText = window.getSelection().toString();
                  }else if(document.getSelection){
                    tmpText = document.getSelection().toString();
                  }else if(document.selection){
                    tmpText = document.selection.createRange().text;
                  }
        });
});

function bold(str)
{
        $('textarea').val($('textarea').val().replace(str,'**'+str+'**'));
}
</script>
</head>
<body>
<button type="button" id="btn_bold">bold it</button>
<textarea>AA</textarea>
</body>
</html>

So if you high-light the first letter A and bold it, you’ll get the result **A**A. But if you high-light the second letter A and bold it, you still get **A**A, instead of A**A** because the line of code $('textarea').val($('textarea').val().replace(str,'**'+str+'**')); is insufficient at identifying which letter A you want to bold.

What’s an efficient way to identify high-lighted text and bold 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-22T17:08:11+00:00Added an answer on May 22, 2026 at 5:08 pm

    I’ve written a jQuery plug-in that does this that I can extract out to a standalone script if it would help. An example of using the plugin to replace selected text with the word “hello”.

    <html>
    <head>
    <script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
    <script type="text/javascript" language="javascript" src="http://rangyinputs.googlecode.com/files/textinputs_jquery.js"></script>
    <script type="text/javascript" language="javascript">
    
    $(document).ready(function() {
    
            $('textarea').bind('mouseup',function() { $(this).replaceSelectedText('hello');});
    });
    </script>
    </head>
    <body>
    <textarea style="width:300px; height:300px;">
    AAA
    </textarea>
    </body>
    </html>
    

    Also, as I commented in a related question of yours, the function you have there won’t work for textareas, which have a separate mechanism for dealing with selections. See my answer there for a function that will work.

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

Sidebar

Related Questions

Sometimes I want to build Python or GCC from scratch just for fun, but
I want to develop simple Serverless LAN Chat program just for fun. How can
I want to create an elevator simulator, just for fun! Will it be beneficial
I am (just for fun) trying to implement a High Score web-service. I would
So I'm coding Pacman in Python just for fun and I want to create
I'm want to create a CRM app for me just for fun and to
I'm programming a little Twitter Client just for fun. I have the tweet's text
I have 160 bits of random data. Just for fun, I want to generate
I am attempting to build a TokenStream from a Python Sequence. Just for fun
I'm making a simple fractal viewing app for Android, just for fun. I'm also

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.