javascript - Highlight text of given position inside div tags -
i have position of selected word using below code:
function getselectioncharoffsetswithin(element) { var start = 0, end = 0; var sel, range, priorrange; if (typeof window.getselection != "undefined") { range = window.getselection().getrangeat(0); priorrange = range.clonerange(); priorrange.selectnodecontents(element); priorrange.setend(range.startcontainer, range.startoffset); start = priorrange.tostring().length; end = start + range.tostring().length - 1; } else if (typeof document.selection != "undefined" && (sel = document.selection).type != "control") { range = sel.createrange(); alert(range); priorrange = document.body.createtextrange(); priorrange.movetoelementtext(element); priorrange.setendpoint("endtostart", range); start = priorrange.text.length; end = start + range.text.length; } return { start: start, end: end } } function alertselection() { var maindiv = document.getelementbyid("myclass"); var sel = getselectioncharoffsetswithin(maindiv); alert(sel.start + ": " + sel.end); }
<div id="myclass" onmouseup="alertselection()"> <p>this highlight text demo.</p> </div>
what want highlight text of position in html text. whatever position give text must highlight in content. if user refresh page should not disappear.
Comments
Post a Comment