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 8152795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:49:09+00:00 2026-06-06T15:49:09+00:00

I have such html piece of code: <ul class=myClass> <li><a href=url1.html>first link</a></li> <li><a href=url2.html>second

  • 0

I have such html piece of code:

<ul class="myClass">
    <li><a href="url1.html">first link</a></li>
    <li><a href="url2.html">second link</a></li>
    <li><a href="url2.html">third link</a></li>
    <li><a href="url3.html">fourth link</a></li>
</ul>

The idea is that when I press the link, it should write to the pressed link next property string: class=”active”. For instance, if we have

<li><a href="url1.html">first link</a></li>`

and I press it, the jQuery function should change it to:

<li><a class="active" href="url1.html">first link</a></li>

But my jQuery function doesn’t really work. I’ll very grateful if you can help me.
Here is source of jQuery function:

$(document).ready(function(){
   var currentUrl = document.location.pathname;
   $('#myClass').each(function() {
       if ( currentUrl == $(this).children('a').attr('href')) {
           $(this).children('a').addClass('active');
       }
   });
});

UPDATE
I put log console.log(currentUrl +"=="+ $(this).children('a').attr('href'));
and it said that the state is undefined. I think the “if” condition is not executed, because, I put and else condition and put and alert there, so any pressing calls alert and if condition is never executed.

  • 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-06T15:49:10+00:00Added an answer on June 6, 2026 at 3:49 pm

    What you’re trying to achieve, could be shortly written like this:

    $(document).ready(function(){        
         var curl = document.location.pathname;  //current url
         curl = curl.substr( curl.lastIndexOf('/')+1 );
         $('.myClass a[href="'+curl+'"]').addClass('active');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such a piece of html: <li class=addToFriends><a href=....>Something something<span>INSIDE SPAN</span>something</a></li> To handle
I have such content of html file: <a class=bf title=Link to book href=/book/229920/>book name</a>
I have such html and css. <div class=selected> <div class=text>First</div> <div class=arrow>&nbsp;</div> </div> .selected
I have a piece of javascript code that I wrote to grab the html
I have a JavaScript widget (a piece of embedded JS and HTML code) that's
I have such html: <legend class="green-color"><a name="section1">Section</a></legend> legend.green-color{ color:green; } In my case Section
I have such html data: <div class=class1>...</div> <div class=class1 class2>...</div> <div class=class3>...</div> ... <div
assume I have a such html order <div id=header>>Header(logo, navigation menus etc)will be here,
I have such files to parse (from scrapping) with Python: some HTML and JS
I have a HTML form with fields such as name, address, notes, etc. 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.