Apply span to selected text in javascript -


i apply span class selected text on browser window in javascript (like highlight feature).

i've tried with

function replaceselectedtextwithhtmlstring(newstring) {     var range = window.getselection().getrangeat(0);     range.deletecontents();     range.innerhtml = newstring; } 

but not work. if try insert newstring= "< span = "myspan" > text < / span >" can't , 'text' not appear. seems not html code. how can solve it?

how easy depends on need achieve. if need basic highlighting using background colour, best bet document.execcommand(). see following code this: change css of selected text using javascript

if need apply more styling document.execcommand() can provide (there various other formatting commands things bold , italic, markup produces varies between browsers , isn't css based), it's trickier: in general, need surround every text node within selection in span desired class. suggest using rangy , css class applier module in cross-browser way. disclaimer: plug own library.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -