jquery find closest previous sibling with class
Try:
$('li.current_sub').prevAll("li.par_cat:first");
Tested it with your markup:
$('li.current_sub').prevAll("li.par_cat:first").text("woohoo");
will fill up the closest previous li.par_cat
with "woohoo".
Try
$('li.current_sub').prev('.par_cat').[do stuff];
Using prevUntil() will allow us to get a distant sibling without having to get all. I had a particularly long set that was too CPU intensive using prevAll().
var category = $('li.current_sub').prev('li.par_cat');
if (category.length == 0){
category = $('li.current_sub').prevUntil('li.par_cat').last().prev();
}
category.show();
This gets the first preceding sibling if it matches, otherwise it gets the sibling preceding the one that matches, so we just back up one more with prev() to get the desired element.