Darcy Darcy - 1 year ago 187
Javascript Question

jQuery add active class to parent on page load

enter image description here
(All child is active as shown in the picture)
I want to add class="active" to parent when its child is clicked. I manage to add class "active" to parent but all the child is added class= "active" too. What i want is only the chosen child and its parent is "active".


var pathname = window.location.pathname;
var filename=pathname.replace(/^.*[\\\/]/, '');

var currentLink=$('a[href="' + filename + '"]'); //Selects the proper a tag
currentLink.parents('.doctormenu').find('li a').removeClass('active');
currentLink.parentsUntil('.doctormenu', 'li').addClass('active');


<ul class="doctormenu">
<li><a href="index.html" class="active">home</a></li>
<li><a href="testabout.php">about</a></li>
<li><a href="testdisease.php">Disease</a></li>
<li><a href="login.html">Search</a></li>

<li><a href="#" >
<li><a href="asdqq.html">Self Diagnosis</a></li>
<li><a href="testservice.php">Online Consultation</a></li>
<li><a href="#">Clinic Appointment</a></li>

Answer Source
 $('.doctormenu li a').each(function(){
        // and test its normalized href against the url pathname regexp
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download