473,587 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting the class of an object on load in jQuery

59 New Member
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_service s" class="current" >Services</li> after the change). How would I do this using jQuery?
Jan 30 '09 #1
4 4101
acoder
16,027 Recognized Expert Moderator MVP
You can get #services using the location.hash property. Then to add a class, use the addClass method.
Jan 30 '09 #2
metaphysics
59 New Member
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 Recognized Expert Moderator MVP
Using the ID selector #nav_services.
Jan 31 '09 #4
metaphysics
59 New Member
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
11843
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
1614
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 automatically load that script with each page I visit?
1
1895
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 www.LearningjQuery.com, share their knowledge, experience, and enthusiasm about jQuery to help users get the most from the library and to make their web...
2
5718
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="<%= ViewData %>" /> -
26
3128
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 of momentum, can it be considered a reasonable choice for those who want to use a full-featured, well supported library? <URL:...
53
8357
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 language="javascript" type="text/javascript">
3
12531
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 of "correct" and then select the first one out of the list. $("div .card:not(.correct):nth-child("1") .question ").css('display', 'none'); After...
0
1369
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: http://idea-palette.com/official/newofficialsite4.html What I am trying to do is, when a user clicks on another Main Nav button, such as 'Web Projects', the background-image changes to...
0
1447
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: 1) the scrollable will function if I directly view PageToLoad.asp in the broswer. 2) the static elements of the scrollable will load onto...
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.