snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#21503

JavaScript

do something on document click, but exclude a particular div and its decendants

1
2
3
4
5
6
7
$(document).click(function(event) {
        var target = $(event.target);
        
        if (!target.attr('id').match(/^sp/) && target.parents('#sp').length == 0) {
            closePopup();
        }
    });
https://snipt.net/embed/b323a1302f5a737dd2308635939c5656/
https://snipt.net/raw/b323a1302f5a737dd2308635939c5656/
b323a1302f5a737dd2308635939c5656
js
JavaScript
7
2014-04-18T03:16:49
True
False
/api/public/snipt/21503/
do-something-on-document-click-but-exclude-a-particular-div-and-its-decendants
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">target</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">target</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">target</span><span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">).</span><span class="nx">match</span><span class="p">(</span><span class="sr">/^sp/</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nx">target</span><span class="p">.</span><span class="nx">parents</span><span class="p">(</span><span class="s1">&#39;#sp&#39;</span><span class="p">).</span><span class="nx">length</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">closePopup</span><span class="p">();</span> </span><span id="L-6"><a name="L-6"></a> <span class="p">}</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">});</span> </span></pre></div> </td></tr></table>
jquery
#13772

JavaScript

ignore a div on body click

1
2
3
4
5
6
7
8
$('body').click(function() {
  console.log("BODY");
});

$('#some-div-to-ignore').click(function(e) {
  e.stopPropagation();
  console.log("IGNORE ME");
});
https://snipt.net/embed/7ae868f51380cb4c2cbeb721ddfa6b9f/
https://snipt.net/raw/7ae868f51380cb4c2cbeb721ddfa6b9f/
7ae868f51380cb4c2cbeb721ddfa6b9f
js
JavaScript
8
2014-04-24T19:47:39
True
False
/api/public/snipt/13772/
ignore-a-div-on-body-click
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;BODY&quot;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="p">});</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#some-div-to-ignore&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="nx">e</span><span class="p">.</span><span class="nx">stopPropagation</span><span class="p">();</span> </span><span id="L-7"><a name="L-7"></a> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;IGNORE ME&quot;</span><span class="p">);</span> </span><span id="L-8"><a name="L-8"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
javascript, jquery
#12624

JavaScript

instant upgrade

1
2
3
4
5
if (window['jQuery'] == undefined || window['jQuery']().jquery.match('1.2') || window['jQuery']().jquery.match('1.3') ) {
    scriptFile=document.createElement("SCRIPT");
    scriptFile.src="http://code.jquery.com/jquery-latest.pack.js"; //Upgrade a brotha
    document.getElementsByTagName("HEAD")[0].appendChild(scriptFile);
    }
https://snipt.net/embed/41b23d772ee23ba845cd6a9d2d01f309/
https://snipt.net/raw/41b23d772ee23ba845cd6a9d2d01f309/
41b23d772ee23ba845cd6a9d2d01f309
js
JavaScript
5
2014-04-16T03:03:10
True
False
/api/public/snipt/12624/
instant-upgrade
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">if</span> <span class="p">(</span><span class="nb">window</span><span class="p">[</span><span class="s1">&#39;jQuery&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="kc">undefined</span> <span class="o">||</span> <span class="nb">window</span><span class="p">[</span><span class="s1">&#39;jQuery&#39;</span><span class="p">]().</span><span class="nx">jquery</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="s1">&#39;1.2&#39;</span><span class="p">)</span> <span class="o">||</span> <span class="nb">window</span><span class="p">[</span><span class="s1">&#39;jQuery&#39;</span><span class="p">]().</span><span class="nx">jquery</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="s1">&#39;1.3&#39;</span><span class="p">)</span> <span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">scriptFile</span><span class="o">=</span><span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s2">&quot;SCRIPT&quot;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">scriptFile</span><span class="p">.</span><span class="nx">src</span><span class="o">=</span><span class="s2">&quot;http://code.jquery.com/jquery-latest.pack.js&quot;</span><span class="p">;</span> <span class="c1">//Upgrade a brotha</span> </span><span id="L-4"><a name="L-4"></a> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s2">&quot;HEAD&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">].</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">scriptFile</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a> <span class="p">}</span> </span></pre></div> </td></tr></table>
jquery