Jquery get and set css attribute of some part of html string

1 answers
Thread Answer
Kubilay
I'm writing an html editor control. I want to change css attributes of selected text of the content. I get the selectedcontent by window.getSelection(); What is the best way to make bold selected text, and to understand selected text is bold then to make it normal. Replace with required tags doesn't work. Since similar texts can be in content. It looks quite complicated to me. I'm looking for the best practise to do it.
Mark

Try this sample snippet

You can make bold as selected text via "document.execCommand('bold');"

Html

<div contenteditable="true" class="textEditor">Text1.
    Text2.
    Text3.
    Text4.
    Hi, this is text of some kind
Lorem ipsum...
</div>

  1. Catch selection change event by jquery-selectionchange event.
  2. Make bold and normal by document.execCommand('bold');

$(document).on('selectionchange', function(e) { 
            document.execCommand('bold');
    });