Solved

jquery targeting parent to add class

Posted on 2014-01-02
1
517 Views
Last Modified: 2014-01-03
I have the following code here Trying to figure out how to add a class named active to the list tag.  

<ul id='sub-nav'>
<li><a href='#one'>Item One</a></li>
<li><a href='#two'>Item Two</a></li>
<li><a href='#three'>Item Three</a></li>
<li><a href='#four'>Item Four</a></li>
</ul>

 $("#sub-nav a[href='#one'']").click(function(){
    /*Trying to figure out how to add a class named active to the list tag */
})

Open in new window

0
Comment
Question by:stargateatlantis
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39753000
for example : http://jsfiddle.net/5N9Xg/1/
$("#sub-nav a[href='#one']").click(function(){
   //$(this).parent().addClass("active"); // <- work too
   $(this).closest("li").addClass("active");
});

Open in new window


Another one : http://jsfiddle.net/5N9Xg/2/
$("#sub-nav a").click(function() {
   $("#sub-nav li").removeClass("active");
   $(this).closest("li").addClass("active");
});

Open in new window

0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question