Skip to content

Instantly share code, notes, and snippets.

@mannuelf
Forked from anonymous/index.html
Created April 22, 2016 10:03
Show Gist options
  • Save mannuelf/dc5b4f2e2bca32b4e9242298d1fdeaeb to your computer and use it in GitHub Desktop.
Save mannuelf/dc5b4f2e2bca32b4e9242298d1fdeaeb to your computer and use it in GitHub Desktop.
PushStack demo jQuery pushStack() demo // source http://jsbin.com/wicama
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="jQuery pushStack() demo">
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>PushStack demo</title>
</head>
<body>
<h1>PushStack Demo</h1>
<input type="button" value="Click me" id="clickme" />
<div id="out1">1</div>
<div id="out2">2</div>
<div id="out3">3</div>
<div id="out4">4</div>
<div id="out5">5</div>
<div id="out6">6</div>
<div id="out7">7</div>
<script id="jsbin-javascript">
$(function() {
$.fn.everyThird = function() {
var arr = [];
$.each(this, function(idx, item) {
if(idx % 3 === 0) {
arr.push(item);
}
});
return this.pushStack(arr, 'everyThird', '');
};
$('#clickme').click(function() {
$('div').everyThird().css('color', 'red').end().css('font-weight', 'bold');
});
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">$(function() {
$.fn.everyThird = function() {
var arr = [];
$.each(this, function(idx, item) {
if(idx % 3 === 0) {
arr.push(item);
}
});
return this.pushStack(arr, 'everyThird', '');
};
$('#clickme').click(function() {
$('div').everyThird().css('color', 'red').end().css('font-weight', 'bold');
});
});</script></body>
</html>
$(function() {
$.fn.everyThird = function() {
var arr = [];
$.each(this, function(idx, item) {
if(idx % 3 === 0) {
arr.push(item);
}
});
return this.pushStack(arr, 'everyThird', '');
};
$('#clickme').click(function() {
$('div').everyThird().css('color', 'red').end().css('font-weight', 'bold');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment