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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:27:13+00:00 2026-05-15T21:27:13+00:00

I’m trying to make a WYSIWYG editor, and so far I have it so

  • 0

I’m trying to make a WYSIWYG editor, and so far I have it so you can select text and click ‘Make Bold’ to make the selected text bold. It literally just wraps < b> (no space) tags around the selected text. But my problem is that if I want to un-bold that, my script has some trouble…

So far, here is my script:

<script language="javascript">
function format(tag) //defines function format
{
    var editor = document.getElementById('editor');
    var txt = '';
    var tester = document.getElementById('tester');
     if (window.getSelection) //if your browser uses this method of text selection
    {
        txt = window.getSelection(); 
             }
    else if (document.getSelection) //if your browser uses this method of text selection
    {
        txt = document.getSelection();
            }
    else if (document.selection) //if your browser uses this method of text selection
    {
        txt = document.selection.createRange().text;
            }
    else return; //Return this
    matched = editor.innerHTML.match(txt); //Find the selected text in the editor
//     if (matched.style.font-weight = "600") {tester.innerHTML = "already bold";} //if the selected text is bold, say 'already bold' DOES NOT WORK
//     else {tester.innerHTML = "not bold";} //if it doesn't...
    editor.innerHTML = editor.innerHTML.replace(matched,"<"+tag+">"+matched+"</"+tag+">");//Wrap <b> tags around it
}
</script>
<input type="button" value="Make Bold" onmousedown="format('b')"> 
<input type="button" value="Make Italic" onmousedown="format('i')"> 
<div id='editor' onclick="javascript:this.designMode='on';" designmode="on" contenteditable="true">Edit Box</div>

<span id="tester">testing span</span>

If you try it out you can type in that box and select text and click Make Bold and it will be bold. Now click Make Bold again but nothing happens. It’s just adding another < b> tag around the selected text. I want it to make it un-bold; normal.

How do I do that?

Thanks 🙂

  • 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-15T21:27:13+00:00Added an answer on May 15, 2026 at 9:27 pm

    Messing with the HTML as a string is a bad idea. There are two better options: the first is to obtain the element containing the current user selection and use DOM methods and properties such as parentNode to test if it’s bold. This is tricky to do cross-browser. Much easier is to use the execCommand method of document, which will automatically toggle boldness. It’s supported in recent versions of all major browsers.

    document.execCommand("bold", false, null);
    

    UPDATE

    Note that in Firefox (and possibly other browsers), this has no effect unless the document has designMode switched on. Here’s a full example. Highlight some text and press Ctrl-B:

    <html>
    <head>
        <title>Test</title>
        <script type="text/javascript">
            window.onload = function() {
                document.designMode = "on";
            };
    
            function keyDown(evt) {
                if (evt.keyCode == 66 && evt.ctrlKey) {
                    document.execCommand("bold", false, "");
                }
                return false;
            }
        </script>
    </head>
    <body onkeydown="return keyDown(event);">
        <div>I like tea <b>with milk</b></div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have just tried to save a simple *.rtf file with some websites and
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

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.