Solved

jquery targeting parent to add class

Posted on 2014-01-02
1
485 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now