Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luislobo14rap/bc3fdf9843b8a0c9d14fa59d729db96c to your computer and use it in GitHub Desktop.
Save luislobo14rap/bc3fdf9843b8a0c9d14fa59d729db96c to your computer and use it in GitHub Desktop.
jQuery.fn.extend({
indexOf: function(selector) {
let thisElement = $(this);
if ($(this).constructor == $().constructor) {
if (typeof selector == 'undefined') {
return $(this).index();
};
if (!$(this).is(selector)) {
console.error('This element is not equal to the selector.');
return stopError;
};
let index = -1;
$(this).parent().children().each(function() {
if ($(this).is(selector)) {
index++;
};
if ($(this).is(thisElement)) {
return false;
};
});
return index;
} else {
console.error('The "indexOf" function only accepts jquery elements.');
return stopError;
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment