I’m new to using ‘this’, but if I am understanding this correctly, I can only use this inside a function defined within another.
In this case I am selecting a li item based on its a href and I need to add a class if it meets the criteria. I feel like I’m close but missing something small, ‘url[1]’ is from a split.
if ($('#Navbar li a:contains(' + url[1] + ')').attr('href')) {
function () {
$(this).parent().addClass('active');
}
}
EDIT:
I am looping through my Li objects based and comparing the href’s to look for a match. Thanks to everyone for helping me through this. I have a much better understanding of this now:
$('#Navbar li a').each(function () {
if ($(this).attr('href')==url[1]) {
$(this).parent().addClass('active');
}
});
The loop was the trick. Thanks: @roasted @brenjt @Maverick