When performing a test like .hasClass()
on a selector which returns multiple elements, it will only perform the test on the first element, so $("#left_menu ul li a").hasClass('active')
is equivalent to $("#left_menu ul li a").first().hasClass('active')
, so it will only ever resolve to true if the first link has the active
class.
If all you need is to determine if one (or more) of the links has the active
class, just check the length of the returned collection when targeting the active
class (working example):
if($("#left_menu ul li a.active").length > 0)
{
console.log($("#left_menu ul li a.active").length);
}
If you need to know the actual link that has the active
class use .each()
to iterate over the collection (working example):
$("#left_menu ul li a").each(function()
{
if($(this).hasClass('active'))
{
console.log($(this).attr('href'));
}
});