0

I wrote a function to highlight a text which is selected using following method,

$('.highlight').click(function(){
        document.execCommand("insertHTML", false, "<span style=background-color:yellow;>"+ document.getSelection()+"</span>");
    });

but, now i needs to unhighlight the selected text, could you please help me.

Thanks in advance.

Gugan Abu
  • 546
  • 1
  • 4
  • 17

1 Answers1

0

CSS

.highlighted{
background-color:yellow;
}

JS

    $('.highlight').click(function(){
        document.execCommand("insertHTML", false, "<span class='selectedText highlighted'>"+ document.getSelection()+"</span>");
    });

    // CALL THIS FUNCTION, WHEN YOU NEED TO UNHIGHLIGHT
    function unhighlight(){
        $(".selectedText").removeClass("highlighted");
    }
anurag
  • 2,176
  • 2
  • 22
  • 41