Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using jQuery, like this (followed by a solution that doesn't use jQuery except for the event; lots fewer function calls, if that's important):</p> <pre><code>$(".rightArrow").click(function() { var rightArrowParents = []; $(this).parents().addBack().not('html').each(function() { var entry = this.tagName.toLowerCase(); if (this.className) { entry += "." + this.className.replace(/ /g, '.'); } rightArrowParents.push(entry); }); alert(rightArrowParents.join(" ")); return false; }); </code></pre> <p><strong>Live example:</strong></p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$(".rightArrow").click(function() { var rightArrowParents = []; $(this).parents().addBack().not('html').each(function() { var entry = this.tagName.toLowerCase(); if (this.className) { entry += "." + this.className.replace(/ /g, '.'); } rightArrowParents.push(entry); }); alert(rightArrowParents.join(" ")); return false; });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="lol multi"&gt; &lt;a href="#" class="rightArrow" title="Next image"&gt;Click here&lt;/a&gt; &lt;/div&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre> </div> </div> </p> <p>(In the live examples, I've updated the <code>class</code> attribute on the <code>div</code> to be <code>lol multi</code> to demonstrate handling multiple classes.)</p> <p>That uses <a href="http://api.jquery.com/parents/" rel="noreferrer"><code>parents</code></a> to get the ancestors of the element that was clicked, removes the <code>html</code> element from that via <a href="http://api.jquery.com/not/" rel="noreferrer"><code>not</code></a> (since you started at <code>body</code>), then loops through creating entries for each parent and pushing them on an array. Then we use <a href="http://api.jquery.com/addBack" rel="noreferrer"><code>addBack</code></a> to add the <code>a</code> back into the set, which also changes the order of the set to what you wanted (<code>parents</code> is special, it gives you the parents in the reverse of the order you wanted, but then <code>addBAck</code> puts it back in DOM order). Then it uses <code>Array#join</code> to create the space-delimited string.</p> <p>When creating the entry, if there's anything on <code>className</code> we replace spaces with <code>.</code> to support elements that have more than one class (<code>&lt;p class='foo bar'&gt;</code> has <code>className</code> = <code>"foo bar"</code>, so that entry ends up being <code>p.foo.bar</code>).</p> <p>Just for completeness, this is one of those places where jQuery may be overkill, you can readily do this just by walking up the DOM:</p> <pre><code>$(".rightArrow").click(function() { var rightArrowParents = [], elm, entry; for (elm = this; elm; elm = elm.parentNode) { entry = elm.tagName.toLowerCase(); if (entry === "html") { break; } if (elm.className) { entry += "." + elm.className.replace(/ /g, '.'); } rightArrowParents.push(entry); } rightArrowParents.reverse(); alert(rightArrowParents.join(" ")); return false; }); </code></pre> <p><strong>Live example:</strong></p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$(".rightArrow").click(function() { var rightArrowParents = [], elm, entry; for (elm = this; elm; elm = elm.parentNode) { entry = elm.tagName.toLowerCase(); if (entry === "html") { break; } if (elm.className) { entry += "." + elm.className.replace(/ /g, '.'); } rightArrowParents.push(entry); } rightArrowParents.reverse(); alert(rightArrowParents.join(" ")); return false; });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="lol multi"&gt; &lt;a href="#" class="rightArrow" title="Next image"&gt;Click here&lt;/a&gt; &lt;/div&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre> </div> </div> </p> <p>There we just use the standard <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1060184317" rel="noreferrer"><code>parentNode</code> property</a> of the element repeatedly to walk up the tree until either we run out of parents or we see the <code>html</code> element. Then we reverse our array (since it's backward to the output you wanted), and join it, and we're good to go.</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. 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