Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jquery targeting parent to add class

Posted on 2014-01-02
1
Medium Priority
?
581 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 83

Accepted Solution

by:
leakim971 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

783 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