Skip to content Skip to sidebar Skip to footer

Using Contenteditable In Firefox: 'bold' Renders Correctly, But Html Code Is Incorrect

I'm trying to write a wysiwyg editor using a contentEditable div, and am having trouble in Firefox when dealing with bold (and italic). When all text in the div is selected, execCo

Solution 1:

Here's a jsFiddle for your original code: http://jsfiddle.net/Bv2Ek/

The issue is that Firefox is adding font-weight: bold to the style attribute of the editable <div> element to make all of it bold, which is quite a reasonable thing to do. If you would rather it used <b> or <strong> elements to apply the boldness, you can use the StyleWithCSS command first. Add the following to your script:

functionbold() {
    document.execCommand('StyleWithCSS', false, false);
    document.execCommand('bold', false, null);
}

And your button becomes:

<buttononClick="bold(); output.value=input.innerHTML;">Bold</button>

jsFiddle example with amended code: http://jsfiddle.net/Bv2Ek/1/

Solution 2:

If... that's IF using jQuery.

After exec command:

$('b').contents().unwrap().wrap('<strong/>');

Post a Comment for "Using Contenteditable In Firefox: 'bold' Renders Correctly, But Html Code Is Incorrect"