Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit:</strong> Since you need this for a FF extension you can skip all the functions for IE and everything you need to do is this:</p> <pre><code>window.getSelection().getRangeAt(0).commonAncestorContainer </code></pre> <p>More information about Ranges: <a href="https://developer.mozilla.org/en/DOM/range" rel="nofollow noreferrer">https://developer.mozilla.org/en/DOM/range</a></p> <hr> <p>There already is a similar question:</p> <p><a href="https://stackoverflow.com/questions/361130/get-selected-text-and-selected-nodes-on-a-page/364476#364476">Get selected text and selected nodes on a page?</a></p> <p>I modified the functions a bit:</p> <pre><code>&lt;script type="text/javascript"&gt; function getTextSelection() { if(document.selection) return document.selection; else if(window.getSelection) return window.getSelection(); else if(document.getSelection) return document.getSelection(); else return false; } function getSelectionRange() { var selection = getTextSelection(); if(selection.getRangeAt) return selection.getRangeAt(0); else if (selection.createRange) return selection.createRange(); else return false; } function getSelectionParent(r) { if(r.parentElement) return r.parentElement; else if(r.commonAncestorContainer) return r.commonAncestorContainer; else return false; } &lt;/script&gt; </code></pre> <p>HTML:</p> <pre><code>&lt;body&gt; &lt;p&gt;&lt;em&gt;This is just some random text. &lt;/em&gt;&lt;strong&gt;Select me and then click the button!!!&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="button" value="Parent element?" onclick="alert('The selection\'s parent element is: ' + getSelectionParent(getSelectionRange()).nodeName);" /&gt;&lt;/p&gt; &lt;/body&gt; </code></pre> <p>I tested this stuff in IE6,7 and FF3.0. Worked without any problems. The only thing you must be aware of is that IE unlike FF ignores text nodes. </p> <p>If you want to try it out yourself: <a href="http://dev.freedig.org/files/selection.html" rel="nofollow noreferrer">http://dev.freedig.org/files/selection.html</a></p>
    singulars
    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. 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.
 

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