Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:28:49+00:00 2026-06-04T00:28:49+00:00

i want to assign a class to an a href tag if this his

  • 0

i want to assign a class to an a href tag if this his clicked, in other words, if one address is in the address bar, assign one class to this tag..

I have this styles in my css..

ul#menu li a {
padding: 10px 20px;
font-weight: bold;
text-decoration: none;
line-height: 2.8em;
color: #465c71;
}

ul#menu li a.selected {
padding: 10px 20px;
font-weight: bold;
text-decoration: none;
line-height: 2.8em;
color: #FFFFFF;
}

Where only change the font color..

And this jquery script in the Layout page of my mvc3 project..

I have this function..

<script type="text/javascript">
  $(function () {
      var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
      $("#menu li a").each(function () {
      var addr = $(this).attr("href").lastIndexOf("/") + 1;
      if (addr == pgurl)
          $(this).addClass("selected");
  })
 });
 </script>

Where pgurl is the bold value http://local.host:81/Home/Services (Sure, because i use and works in another jquery function)

And I have a a href link like this..

<li><a href="@Url.Action("Services", "Home")">@MvcWebRole1.Resources.Shared.Layout.Services</a></li>

And this above generates a link as this..

<a href="/Home/Services">Services</a>

Where i want to add a class to this tag if the first variable is the same of page that i am.. I i do through firebug or similar (add manually the class) works as expected..

Finally, two variables should have the bold value(Services), but doesnt add the class at the tag, i work with Visual Studio 2010 and i cant test this values, i cant put any breakpoint on it to view their values.. And as commented above, i have another similar function thats works perfectly with the tags of another list..

Can someone help me?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:28:50+00:00Added an answer on June 4, 2026 at 12:28 am
    var addr = $(this).attr("href").lastIndexOf("/") + 1;
    

    The above assigns a number + 1 to addr. You then go on to test this number is equal to a string so unless your action is a number (not sure if it can be), then it will always fail.

    Maybe you meant to get the substring from the link as well

     var addr = $(this).attr("href").substr($(this).attr("href").lastIndexOf("/") + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an click event that I want to assign to more than class.
I have a declared entity of a class, and want to assign different pre-made
I have a variable: Class<Map.Entry<String, Boolean>> clazz; And I want to assign a class
Let's say I create an instance of a class and want to assign some
I want to assign the array item into variable directly using groovy like this:
I have 4 items in and I want to assign 4 different background-color to
I want to assign a class to the list-items of an ul li list
If I have a class/object like this #import <Foundation/Foundation.h> @interface anObject : NSObject {
I have a base class Element and derived two other classes from it, Solid
I have a abstract class where I want to declare final variables. However, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.