snipt

Ctrl+h for KB shortcuts

JavaScript

jQuery. Steps in $.each()

1
2
3
4
5
6
var eachItem = 4; // someFunction() will work on 1st, 5th, 9th ... element
$('.selector').each(function(i) {
	if(i++ % eachItem === 0) { // to select 4th, 8th etc. elements use ++i instead of i++
		someFunction();
	}
});
https://snipt.net/embed/48ddd2a0e4e231a034ac64ebf3fde8ac/
https://snipt.net/raw/48ddd2a0e4e231a034ac64ebf3fde8ac/
48ddd2a0e4e231a034ac64ebf3fde8ac
js
JavaScript
6
2016-09-29T16:06:12
True
False
False
/api/public/snipt/129927/
jquery-steps-in-each
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">eachItem</span> <span class="o">=</span> <span class="mi">4</span><span class="p">;</span> <span class="c1">// someFunction() will work on 1st, 5th, 9th ... element</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.selector&#39;</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">if</span><span class="p">(</span><span class="nx">i</span><span class="o">++</span> <span class="o">%</span> <span class="nx">eachItem</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// to select 4th, 8th etc. elements use ++i instead of i++</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">someFunction</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a> <span class="p">}</span> </span><span id="L-6"><a name="L-6"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
each, function, hack, jquery

PHP

extract function use case

1
2
3
<?php
	extract($identity); //var_dump(extract($identity)) == int 3
?>
https://snipt.net/embed/49db45f71cc1bd268a8be44bbdcf151a/
https://snipt.net/raw/49db45f71cc1bd268a8be44bbdcf151a/
49db45f71cc1bd268a8be44bbdcf151a
php
PHP
3
2016-09-29T22:21:27
True
False
False
/api/public/snipt/128088/
extract-function-use-case
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> <span class="nb">extract</span><span class="p">(</span><span class="nv">$identity</span><span class="p">);</span> <span class="c1">//var_dump(extract($identity)) == int 3</span> </span><span id="L-3"><a name="L-3"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
extract, function, php

JavaScript

Get Viewport width. jQuery

1
2
3
4
5
6
7
8
function viewportWidth() {
    var $body = $('body'),
        viewPortWidth = $body.css('overflow', 'hidden').width();
    
    $body.css('overflow', '');

    return viewPortWidth;
}
https://snipt.net/embed/9935d9ab42e952161c3411323936eafc/
https://snipt.net/raw/9935d9ab42e952161c3411323936eafc/
9935d9ab42e952161c3411323936eafc
js
JavaScript
8
2016-09-29T16:06:29
True
False
False
Apr 05, 2014 at 03:42 PM
/api/public/snipt/127685/
get-viewport-width-jquery
<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="kd">function</span> <span class="nx">viewportWidth</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">$body</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">),</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">viewPortWidth</span> <span class="o">=</span> <span class="nx">$body</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span><span class="s1">&#39;overflow&#39;</span><span class="p">,</span> <span class="s1">&#39;hidden&#39;</span><span class="p">).</span><span class="nx">width</span><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">$body</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span><span class="s1">&#39;overflow&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a> <span class="k">return</span> <span class="nx">viewPortWidth</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>
function, jquery, simple, width
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.