I want prevent default and add an active class to the parent links of a list in a dropdown menu. The code i have is this:
Markup:
<ul class="subNavMobile">
<li>
<a href="#">link parent</a>
<ul>
<li><a href="anurl.html">child link</a></li>
</ul>
</li>
<li><a href="#">link parent2</a></li>
<li><a href="#">link parent3</a></li>
</ul>
jQuery:
$('.subNavMobile > li a').on('click', function (e) {
var parent = $(this).parent();
if (!parent.hasClass('active')) {
parent.parent().find('li.active').removeClass('active');
parent.addClass('active');
} else {
parent.removeClass('active');
}
});
But this is also affectiong to the child list and this is not desired. How do I only add the class and prevent default in just the parents and not in the child? Thank you.