Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created January 22, 2017 09:49
Show Gist options
  • Save Kcko/8c489234982cd325f6ede449408a0f69 to your computer and use it in GitHub Desktop.
Save Kcko/8c489234982cd325f6ede449408a0f69 to your computer and use it in GitHub Desktop.
jQuery - own methods
$(function(){
jQuery.extend(jQuery.expr[':'], {
'child-of' : function(a,b,c) {
return (jQuery(a).parents(c[3]).length > 0);
}
});
//'child-of' is now a valid selector:
$("li:child-of(ul.test)").css("background","#000");
});
$(function(){
jQuery.fn.isChildOf = function(b){return (this.parents(b).length > 0);};
//Now we can evaluate like this:
if ( $("li").isChildOf("ul") ){
//Obviously, the li is a child of the ul so this code is executed
}
});
$(function(){
jQuery.fn.putBefore = function(dest){
return this.each(function(){
$(dest).before($(this));
});
}
jQuery.fn.putAfter = function(dest){
return this.each(function(){
$(dest).after($(this));
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment