-4

consider below content editable div

.editable-div div {
display: inline-block;
}
<div contenteditable="true" class="editable-div">
  <div> This </div> <div> is a an example</div><div> for inline block</div> <div>elemenst </div> 
</div>
  1. Put mouse cursor on anywhere in the content
  2. Press shift+home
  3. It will select only inline-block div content
  4. I want to select the whole content till beginning of the div
  5. I can not change inline-block style of inner divs
  6. Due to above reason(5) I am doing an custom implementation for shift+home and shift+ end, that is why I need to select specific text from a div

*Select as In text selection (user-select) of browser, I already have links for selecting whole content of a div Selecting text in an element (akin to highlighting with your mouse)

but here I want to select specific text only. please help

sumi
  • 417
  • 3
  • 14

1 Answers1

0

I think window.getSelection() is what you're looking for.

var selection = "";

function getText() {
  if (window.getSelection) {
    selection = window.getSelection().toString();
  } else if (document.selection && document.selection.type !== "Control") {
    selection = document.selection.createRange().text;
  }
  return text;
}
Jay Jordan
  • 643
  • 4
  • 16