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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:09:40+00:00 2026-05-20T21:09:40+00:00

I am having trouble (complete) with execCommand so you help much appreciated – I

  • 0

I am having trouble (complete) with execCommand so you help much appreciated – I have no doubt I am barking up the wrong tree but ….. anyway

I have a div like this

<div class="editable" id="editor" contenteditable="true"> 
    <2>Some text in here</h2> blah blah blah ...
</div>

That is “editable” i.e. document.designMode = 'on'; – it get’s this “state” on focus. On blur it changes to document.designMode = 'off';

I have a “test button”

<input type="button" id="bold" value="Bold"> 

That when “clicked” makes the “highlighted text” bold – hence execCommand

So far I have something like this:

function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}


$('#bold').click(function(){
var selection = getSelected();
alert(selection);
});

The alert (bold click) does give me the highlighted/selected text but I cannot work out how to “turn it” bold. I guess I need to access the innerHTML or something?

Help much appreciated – thanks in advance. OH and I do not want to use either an i-frame or a textarea

  • 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-20T21:09:41+00:00Added an answer on May 20, 2026 at 9:09 pm

    I strongly recommend using Rangy for dealing with text selection.

    A cross-browser JavaScript range and selection library. It provides a simple standards-based API for performing common DOM Range and Selection tasks in all major browsers, abstracting away the wildly different implementations of this functionality between Internet Explorer and DOM-compliant browsers.

    You can use the CSS Class Applier Module to bold the text (live demo).

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

Sidebar

Related Questions

i have used observed_field for arrays of data... but i am having trouble making
I have a design I'm working on that is almost complete, but I'm having
I'm having trouble getting intellisense to auto-complete shared pointers for boost 1.40.0. (It works
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I'm having trouble with a simple threading example. I know my methods aren't complete,
I'm attempting to complete and exercise from the JavaScript Bible, and am having trouble
Perhaps I'm just having trouble figuring out the callbackyness, but I can't figure out
I am a complete newbie to sml and am having trouble with the syntax
I'm having trouble with something that should be so easy, but I don't know
I have been having trouble with timeout on SQL program , I have narrowed

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.