Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In collaboration with @drzaus we've come up with the following jQuery plugin.</p> <h1>jQuery.getSelector</h1> <pre><code>!(function ($, undefined) { /// adapted http://jsfiddle.net/drzaus/Hgjfh/5/ var get_selector = function (element) { var pieces = []; for (; element &amp;&amp; element.tagName !== undefined; element = element.parentNode) { if (element.className) { var classes = element.className.split(' '); for (var i in classes) { if (classes.hasOwnProperty(i) &amp;&amp; classes[i]) { pieces.unshift(classes[i]); pieces.unshift('.'); } } } if (element.id &amp;&amp; !/\s/.test(element.id)) { pieces.unshift(element.id); pieces.unshift('#'); } pieces.unshift(element.tagName); pieces.unshift(' &gt; '); } return pieces.slice(1).join(''); }; $.fn.getSelector = function (only_one) { if (true === only_one) { return get_selector(this[0]); } else { return $.map(this, function (el) { return get_selector(el); }); } }; })(window.jQuery); </code></pre> <h2>Minified Javascript</h2> <pre><code>// http://stackoverflow.com/questions/2420970/how-can-i-get-selector-from-jquery-object/15623322#15623322 !function(e,t){var n=function(e){var n=[];for(;e&amp;&amp;e.tagName!==t;e=e.parentNode){if(e.className){var r=e.className.split(" ");for(var i in r){if(r.hasOwnProperty(i)&amp;&amp;r[i]){n.unshift(r[i]);n.unshift(".")}}}if(e.id&amp;&amp;!/\s/.test(e.id)){n.unshift(e.id);n.unshift("#")}n.unshift(e.tagName);n.unshift(" &gt; ")}return n.slice(1).join("")};e.fn.getSelector=function(t){if(true===t){return n(this[0])}else{return e.map(this,function(e){return n(e)})}}}(window.jQuery) </code></pre> <h2>Usage and Gotchas</h2> <pre><code>&lt;html&gt; &lt;head&gt;...&lt;/head&gt; &lt;body&gt; &lt;div id="sidebar"&gt; &lt;ul&gt; &lt;li&gt; &lt;a href="/" id="home"&gt;Home&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div id="main"&gt; &lt;h1 id="title"&gt;Welcome&lt;/h1&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; // Simple use case $('#main').getSelector(); // =&gt; 'HTML &gt; BODY &gt; DIV#main' // If there are multiple matches then an array will be returned $('body &gt; div').getSelector(); // =&gt; ['HTML &gt; BODY &gt; DIV#main', 'HTML &gt; BODY &gt; DIV#sidebar'] // Passing true to the method will cause it to return the selector for the first match $('body &gt; div').getSelector(true); // =&gt; 'HTML &gt; BODY &gt; DIV#main' &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>Fiddle w/ QUnit tests</h2> <p><a href="http://jsfiddle.net/CALY5/5/" rel="noreferrer">http://jsfiddle.net/CALY5/5/</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.
    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