SkyeBoniwell
SkyeBoniwell

Reputation: 7092

formatting text with document.execCommand and browser compatibility

I've put together a compact little bit of code to change the styling of a selection of text.

It almost seems too simple.

Please see my jsfiddle for reference.

I can get this to work in IE10, and FF, but it won't work in IE9 or Safari.

Is there any way to get it to work with those browsers?

Thanks

jsfiddle: http://jsfiddle.net/Yd3u8/27/

$(".styleEvent").click(function(e) {
var styleType = e.target.id;
styleEvent(styleType);

});

function styleEvent(style) {
    document.execCommand(style);
}

Upvotes: 0

Views: 166

Answers (1)

Hackerman
Hackerman

Reputation: 12305

Try something like this:

function styleEvent(style) {
document.execCommand(style);
if(style=="subscript"){style="line-through";} 
if(style=="superscript"){style="overline";}
if(style=="bold"){document.getElementById('editor').style.fontWeight= "bold";}
document.getElementById('editor').style.textDecoration=style;
}

Working fiddle: http://jsfiddle.net/robertrozas/Yd3u8/30/

Upvotes: 1

Related Questions