473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Setting the class of an object on load in jQuery

I am trying to set the class of a "<li>" based on if the page is loaded with an anchor. For example, if http://www.example.com/index.html#services is loaded, I would like to change the class of li#nav_services to current. (The html would be <li id="nav_services" class="current">Services</li> after the change). How would I do this using jQuery?
Jan 30 '09 #1
4 4095
acoder
16,027 Expert Mod 8TB
You can get #services using the location.hash property. Then to add a class, use the addClass method.
Jan 30 '09 #2
Okay, so if I have:

Expand|Select|Wrap|Line Numbers
  1. $(function(){
  2.     var path = location.pathname.substring(1);
  3.     if ( path )
  4.         $('.navbar a[@href$="' + path + '"]').attr('class', 'current');
  5. });
How do I get it to select the <li> that the <a> is nested in?
Jan 30 '09 #3
acoder
16,027 Expert Mod 8TB
Using the ID selector #nav_services.
Jan 31 '09 #4
Alright, thanks. I figured it out.
Jan 31 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: =?iso-8859-1?q?Jean-S=E9bastien?= | last post by:
Hello, I'm starting in rails and ajax. So i've a javascript question: someone could help me to update the class of a div? Regards.
7
by: Dave Fowler | last post by:
I have a simple script that hides an image when you click on it. I think it would be cool to have that functionality for all pages I visit. Is there a way to set my browser settings to...
1
by: DuaneMoraes | last post by:
A new book on jQuery, the powerful JavaScript library, has been announced by Packt In Learning jQuery, Karl Swedberg and Jonathan Chaffer, creators of the popular jQuery learning resource...
2
by: thj | last post by:
Hi. I've got this form that I'm trying to validate: <form id="periodForm" action="" method="post"> <p> Periode: <input id="startDate" name="startDate" type="text" size="7" value="<%=...
26
by: RobG | last post by:
Do some of the regulars here need to re-think their (sometimes strident) opposition to libraries? Both Microsoft and Nokia have announced support for jQuery. It seems to have gained quite a bit...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
3
by: mberkom | last post by:
I've got a jquery selector I'm trying to get done and since I'm somewhat new to jquery I'm stumped. Essentially. I'm trying to get a div within divs with the class "card" that do not have the class...
0
by: zeckdude | last post by:
Hi, I am having some issues with my site. I have a main Nav with four links that load in 4 different sections. You can see the page I am working on here:...
0
by: liz0001 | last post by:
I have seen many examples on how to use the .load() function to load in static elements, but I am trying to load a dynamic element, specifically a scrollable. I have gotten it to the point where:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.