Skip to content

Instantly share code, notes, and snippets.

@remi
Created May 26, 2010 20:11
Show Gist options
  • Save remi/414981 to your computer and use it in GitHub Desktop.
Save remi/414981 to your computer and use it in GitHub Desktop.
// Instead of using $('div').parent().parent().parent().parent().addClass('lol')
// you can use this: $('div').parentx(4).addClass('lol')
if ( window.jQuery ) {
jQuery.fn.parentx = function( i ) {
if ( i > 1 ) {
var e = $(this);
for ( var a = 1; a <= i; a++ ) {
e = e.parent();
}
return e;
}
return $(this).parent();
}
}
@remi
Copy link
Author

remi commented May 26, 2010

Effectivement, peut-être que le for est un peu gourmand en termes de ressources. Cependant, un index inexistant dans ton [] pourrait causer une erreur. Je teste ça et je reviens avec les résultats ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment