Adding "this" to the parents stack for "each" in jQuery
Posted
by Matrym
on Stack Overflow
See other posts from Stack Overflow
or by Matrym
Published on 2010-04-01T05:10:06Z
Indexed on
2010/04/01
5:13 UTC
Read the original article
Hit count: 596
This question is a bit of a two-parter. First, the title question. Here's what I've got:
// Report all of the parents
$(this).parents().each(function(i){
// Collect the parts in a var
var $crumb = '';
// Get the tag name of the parent
$crumb += "<span class='tagName'>"+this.tagName+"</span>";
// And finally, report it
$breadcrumbs.prepend($crumb);
});
Unfortunately, this doesn't include the actual element itself, only the parents. Is there any way of saying something like "this and parents"?
Now, the second question. If I were unable to add to the stack, how would I separate the guts of that function into another function, while retaining the "this" ability of it? Would it be something like:
// Function to report the findings
function crumble(e){
// Collect the parts in a var
var $crumb = '';
// Get the tag name of the parent
$crumb += "<span class='tagName'>"+this.tagName+"</span>";
// And finally, report it
$breadcrumbs.prepend($crumb);
};
$(this).parents().each(crumble());
Thanks in advance for your time!
© Stack Overflow or respective owner