-2

I have the following problem: I would like to add class="active" to the li matching the webpage I'm surfing on using javascript (I use jQuery library). I can't figure out how to do that, though I tried many examples found here and on other sites. So here is my html:

<header>
  <div id="menu" class="clearfix">
    <nav class="navigation">
      <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="blabla.html">Bla</a></li>
        <li><a href="blablabla.html">Blablabla</a></li>
        <li><a href="blaX4.html">Tools</a></li>
        <li><a href="contact.html">Contact</a></li>
      </ul>
    </nav>
  </div>
</header>

Thank you very much! Any answer will be appreciated!

2

1 Answer 1

0

this should works just fine :)

   $(document).ready(function () {
            var url = window.location;
    $('.navigation ul li a[href="'+url+'"]').parent().addClass('active');
    });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.