Note that there are some explanatory texts on larger screens.

plurals
  1. POInline jQuery; select relative to script tag
    text
    copied!<p>This is a bit goofy, as its counter-intuitive to liberating markup of Javascript, however I'm going to ask anyways.</p> <p>Given this snippet:</p> <pre><code>&lt;p&gt;Hello&lt;/p&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $('relative-selector').next('p').hide(); }); &lt;/script&gt; &lt;p&gt;World&lt;/p&gt; </code></pre> <p>This snippet would target the <code>&lt;script&gt;</code> tag itself with this "<em>relative selector</em>", and <code>.next('p').hide()</code> would result in <code>&lt;p&gt;World&lt;/p&gt;</code> being hidden.</p> <p>Does there exist a "<em>relative selector</em>", or means of targeting the script tag a given snippet resides within?</p> <p>The answer I'm looking for (<em>given such one exists</em>) would not require the use of an <code>id</code> attribute, or any such identifying attributes; it would work with an arbitrary number of <code>&lt;script&gt;</code> tags in a given document, regardless of position in the DOM tree.</p> <p>I've seen some strange implementations that don't use <code>$(document).ready()</code>, instead relying on the fact that the remaining markup has not loaded, using <code>$('script:last')</code> or some such concoction. This isn't what I'm after though; I'd like to <code>.bind()</code> some handlers to elements relative to the binding script snippet (<em>typically after, which is why the unloaded markup trick won't work</em>)</p> <p><code>$(this)</code> simply targets the <code>document</code> object due to the <code>ready</code> handler. <code>$(this)</code> outside of load-deferred handlers targets <code>window</code>.</p> <p>I've already nearly accepted that this probably isn't possible, however I'm sure if any solution exists, its floating about in the minds of fellow SO users.</p>
 

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