The following code block does not work:
$("<span class=\"ideacount\">").prependTo(chapter.parents(".chapter")).each( function(index) {
$(this).html((parseInt($(this).text() || 0)+1)+"");
});
where 'chapter' is a node that in this case is nested 5 elements deep among other elements of class 'chapter'.
Whereas this code behaves as expected, but is not what I want (it prepends to all other branches of the tree structure as well instead of just the ancestry of 'chapter'):
$("<span class=\"ideacount\">").prependTo(".chapter").each( function(index) {
$(this).html((parseInt($(this).text() || 0)+1)+"");
});
Edit: to elaborate:
chapter.parents().length
is 10 (5 spans of class 'chapter', 5 li's)
chapter.parents(".chapter").length is 0!?
Try closest instead of parents.