Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By "editable content window" I'm going to assume you mean an element with <code>contenteditable</code> turned on or a document with <code>designMode</code> turned on.</p> <p>There are also two cases to consider: the case when the user has made a selection and the case where there is just a caret. The code below will work in both cases, and will give you the innermost <strong>element</strong> that completely contains the selection. If the selection is completely contained within a text node it's slightly complicated to get that text node in IE (trivial in other browsers), so I haven't provided that code here. If you need it, I can dig it out.</p> <pre><code>function getSelectionContainerElement() { var range, sel, container; if (document.selection &amp;&amp; document.selection.createRange) { // IE case range = document.selection.createRange(); return range.parentElement(); } else if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt) { if (sel.rangeCount &gt; 0) { range = sel.getRangeAt(0); } } else { // Old WebKit selection object has no getRangeAt, so // create a range from other selection properties range = document.createRange(); range.setStart(sel.anchorNode, sel.anchorOffset); range.setEnd(sel.focusNode, sel.focusOffset); // Handle the case when the selection was selected backwards (from the end to the start in the document) if (range.collapsed !== sel.isCollapsed) { range.setStart(sel.focusNode, sel.focusOffset); range.setEnd(sel.anchorNode, sel.anchorOffset); } } if (range) { container = range.commonAncestorContainer; // Check if the container is a text node and return its parent if so return container.nodeType === 3 ? container.parentNode : container; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload