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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:08:36+00:00 2026-06-15T03:08:36+00:00

In my form I want to allow the user to format text and add

  • 0

In my form I want to allow the user to format text and add lists and such (basic html functionality, without having to know html).

Using the Bold button as an example:

<div class="goBold button">B</div>
<input type="text" name="txtMessage" />
<script>
$(".goBold").click(function() {
    formatText("bold");
});

function formatText(formatType)
{
    var input = $("#txtMessage");
    var text = input.val();
    var ss = input[0].selectionStart;
    var se = input[0].selectionEnd;

    if (formatType == "bold")
    {
        if (ss == se)
        {
            // there's no text in the textbox, so just write in the tags
            input.val(text + "[b][/b]");
        }
        else
        {
            // surround the highlighted text with the tags
            input.val(text.substr(0, ss) + "[b]" + text.substr(ss, se) + "[/b]" + text.substring(se, text.length));
        }
    }
}
</script>

While all this works, there is a slight problem:

Lets assume my text value for this textbox is

The quick red fox jumps over the lazy brown dog. This is the only sentence in the English language that uses all 26 letters of the alphabet

With the words all 26 letters highlighted, clicking the bold button will wrap the text in the tags, but it will also add a second copy of the text that exists after the selection.

I’m not sure why this is, can anyone provide any insight here?

TIA 🙂

  • 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-06-15T03:08:37+00:00Added an answer on June 15, 2026 at 3:08 am

    You should use substring everywhere :

    input.val(text.substring(0, ss) + 
              "[b]" + text.substring(ss, se) + "[/b]" + 
              text.substring(se, text.length));
    

    (BTW, your selector should be $("input[name=txtMessage]") – it has no id).

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

Sidebar

Related Questions

I have a large web form application that I want to allow the user
Before the user closes the form I want to prompt with a confirmation box.
I want to allow my user to store custom phrases, to be displayed in
I want to allow the user to delete a list of associated foreignkeys to
I want to allow a user to select an image from their local filesystem
I want to develop an application that allow user to writes and print cheques.
So I'm using Markdown to format text input from user: http://michelf.com/projects/php-markdown/ But I'm doing
I want to allow a user to enter a list of persons in a
I am working on a Windows Form Application and I want to allow the
As per requirement, I want to allow the user to write to session when

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.