How to remove span with class name around selected text in javascript -
i using javascript highlight selected text , remove selected text when user wants.
have done everything, stuck when comes remove style of selected text.
want remove span class name highlight
around selected text not remove other styles available inside selected text.
here the plunk i've made:
function removehighlight() { var sel = window.getselection(); if (sel.rangecount > 0) { var range = sel.getrangeat(0); var selectedtextnode = document.createtextnode(range.tostring()); console.log(selectedtextnode); range.deletecontents(); range.insertnode(selectedtextnode); range.selectnode(selectedtextnode); sel.removeallranges(); sel.addrange(range); } }
what proper way remove highlighted span around selected text ?
you can use remove
method of classlist
property:
// spans have highlight class var spans = document.queryselectorall("span.highlight"); // loop on spans , remove classs for(var = 0; < spans.length; i++){ spans[i].classlist.remove("highlight"); }
Comments
Post a Comment