public
Last active

  • Download Gist
jquery.withchildren.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Say we got: <ul> <li>Foo</li> <li>Bar</li> </ul>
// Fade list entries one by one, with a 200ms interval
//
// $.withChildren('ul:first', 'li', 'fadeOut', [], 200)
//
 
;(function($) {
$.withChildren = function(element, childSelector, _method, args, after) {
var childTag = childSelector || {'DIV': 'div', 'UL': 'li'}[$(element).get(0).tagName] || 'div';
var children = $(element).find('> ' + childTag);
(function(e) {
$(e)[_method].apply($(e), args);
var self = arguments.callee;
if ($(e).next().length) { setTimeout(function() { self($(e).next()); }, (parseInt(after) || 1000)); };
})(children[0]);
};
})(jQuery);
 
// *cough* documentation:
// $.withChildren(<selector for finding child nodes>, <method called on children>, <arguments for method>, <interval>)
//
// Pluginize it
$.fn.withChildren = function(childSelector, _method, args, after) {
return this.each(function() { $.withChildren(this, childSelector, _method, args, after) });
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.