This is one your are looking for i believe:
text = window.getSelection().toString();
Hide selected/highlighted text javascript
You have to get parent of Element from DOM:
function getSelectionParentElement() { var parentEl = null, sel; if (window.getSelection) { sel = window.getSelection(); if (sel.rangeCount) { parentEl = sel.getRangeAt(0).commonAncestorContainer; if (parentEl.nodeType != 1) { parentEl = parentEl.parentNode; } } } else if ( (sel = document.selection) && sel.type != "Control") { parentEl = sel.createRange().parentElement(); } return parentEl; }
Update
Fixed demo to hide text we have to find startOffset
function getStartOffset() { var sel = document.selection, range, rect; var x = 0, y = 0; if (sel) { if (sel.type != "Control") { range = sel.createRange(); range.collapse(true); } } else if (window.getSelection) { sel = window.getSelection(); if (sel.rangeCount) { range = sel.getRangeAt(0).cloneRange(); if (range.getClientRects) { range.collapse(true); } } } return range.startOffset; }
and how to hide highlighted text?as op needs this.