JavaScript document.execCommand() own tags

YeppThat'sMe picture YeppThat'sMe · May 13, 2013 · Viewed 9.6k times · Source

I've found some really interesting! The execCommand function applies many useful features. But is it possible to work with OWN wraps? Like:

document.execCommand("styleWithCSS", false, "<span class='own-class'>");

Everything related i've found was pretty old… so maybe one of you knows a good workaround or something.

Answer

YeppThat&#39;sMe picture YeppThat'sMe · May 13, 2013

Thanks to dandavis.

The following works very well:

document.execCommand("insertHTML", false, "<span class='own-class'>"+ document.getSelection()+"</span>");