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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:02:47+00:00 2026-05-24T21:02:47+00:00

I´m pretty new with Jquery, so I´m not really sure what functions I should

  • 0

I´m pretty new with Jquery, so I´m not really sure what functions I should be using for my case.

I have a list of links, like so:

<ul class="topmenu">
           <li class="selected"><a href='index.html'>Hem</a></li>
           <li class=""><a href='services.html'>Tjänster </a></li>
           <li class=""><a href='about.html'>Om oss </a></li>
           <li class=""><a href='vaccancies.html'>Lediga jobb </a></li>
           <li class=""><a href='contact.html'>Kontakta oss </a></li>
</ul>

This is part of my footer, and I´m including this footer in all my pages using jquery load:

$('#footer_include').load('footer.html');

Now, my issue is that I want to be able to dynamically set the class “selected” to my list items, based on what page the user is currently on. So if the user is on vaccancies.html, I want the class for that list item to be set to “selected”. So I´m guessing something like

$(document).ready(function () {
     ...
             $(this).addClass('selected');
     ...

});

But as you can see, my code is incomplete… Can someone help me finish it? I guess I might need to add IDs to the individual list items aswell?

Cheers!

  • 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-05-24T21:02:48+00:00Added an answer on May 24, 2026 at 9:02 pm

    Add custom data attributes

    <ul class="topmenu">
        <li data-pathname="/index.html" class="selected"><a href='index.html'>Hem</a></li>
        <li data-pathname="/services.html" class=""><a href='services.html'>Tjänster </a></li>
        <li data-pathname="/about.html" class=""><a href='about.html'>Om oss </a></li>
        <li data-pathname="/vaccancies.html" class=""><a href='vaccancies.html'>Lediga jobb </a></li>
        <li data-pathname="/contact.html" class=""><a href='contact.html'>Kontakta oss </a></li>
    </ul>
    

    JQuery:

    $('li[data-pathname="' + window.location.pathname + '"]').addClass('selected');
    

    Example: http://jsfiddle.net/AlienWebguy/CstXK/

    If you wanted to use PHP to set the class, you could do this:

    <?php
    
      $selected['index']      = ($_SERVER['REQUEST_URI'] == '/index.html')      ? $_SERVER['REQUEST_URI'] : '';
      $selected['services']   = ($_SERVER['REQUEST_URI'] == '/services.html')   ? $_SERVER['REQUEST_URI'] : '';
      $selected['about']      = ($_SERVER['REQUEST_URI'] == '/about.html')      ? $_SERVER['REQUEST_URI'] : '';
      $selected['vaccancies'] = ($_SERVER['REQUEST_URI'] == '/vaccancies.html') ? $_SERVER['REQUEST_URI'] : '';
      $selected['contact']    = ($_SERVER['REQUEST_URI'] == '/contact.html')    ? $_SERVER['REQUEST_URI'] : '';
    
    echo <<<HTML
    <ul class="topmenu">
        <li class="{$selected['index']}"><a href='index.html'>Hem</a></li>
        <li class="{$selected['services']}"><a href='services.html'>Tjänster </a></li>
        <li class="{$selected['about']}"><a href='about.html'>Om oss </a></li>
        <li class="{$selected['vaccancies']}"><a href='vaccancies.html'>Lediga jobb </a></li>
        <li class="{$selected['contact']}"><a href='contact.html'>Kontakta oss </a></li>
    </ul>
    HTML;
    

    Of course that PHP could be cleaned up with an array iteration and a substr, etc., but you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to JQuery, and this is probably not the best use case
I am pretty new to the zend framework and the JQuery helper. I have
I'm new to jquery and i'm trying to make a dropdown menu list like
I'm pretty new to jQuery, so please forgive... I have a page with a
I'm pretty new to ajax (via jQuery) and JavaScript. What I would like is
So I am pretty new to both Django and Javascript (I am using JQuery)
I have just entered the world of jquery and am pretty new to javascript
I'm pretty new to javascript and jquery, and have run into a problem that
I am pretty new to bootstrap, jquery and client side. I am using the
I am pretty new to mobile stuff, jquery... Here is the situation I have

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.