Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have some errors in your code, your missing a "}" to close the function.</p> <p>Also this wont return true since the selection is made on a button and not on an anchor (link)</p> <p>Try this code (I understand if this isn't what you want, but it makes your example work)</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function SelectAnchorNode () { if (window.getSelection) { // Firefox, Opera, Google Chrome and Safari var selection = window.getSelection(); if (selection.anchorNode) { var rangeToSelect = document.createRange(); alert(selection.anchorNode.nodeName); } else { alert ("Your browser does not support this example!"); } } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a onclick="SelectAnchorNode();"&gt;Click to check!&lt;/a&gt; &lt;br /&gt;&lt;br /&gt; &lt;div&gt; &lt;div&gt;This is the first line.&lt;/div&gt; &lt;p&gt;This is the second line.&lt;/p&gt; &lt;/div&gt; </code></pre> <p> </p> <p>As you can see I replaced "button" with "a", and added a missing "}"</p> <p>EDIT!<br> Try this line instead, (I have now assumed that you want to get the selection made by the user, and that you don't want to create a selection with javascript?) </p> <pre><code>alert(selection.anchorNode.parentNode.nodeName); </code></pre> <p>Depending on where you start your selection you will get different results, you also need to first check if there was a text node selected or not (since I could make a selection directly in body). But I hope this brings you closer to your solution.</p> <p>Regards<br> Tobias</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. 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