treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Highlight All Links To Current Page

Last updated on:

$(function(){
       $("a").each(function(){
               if ($(this).attr("href") == window.location.pathname){
                       $(this).addClass("selected");
               }
       });
});

This function will add the class "selected" to any links (even relative) that point to the current page.

View Comments

Comments

  1. Permalink to comment#

    I’m looking for a script that has a menu that shows what part of the page ur on. EX:

    LINK 1 LINK2 LINK3 (bold cause ur on that area)

    LINK 3 Area

  2. This is exactly what I’m looking for, but I’m unsure how to deploy it…
    Does this need to go in Script tags in the Head section of my page?
    How do I define the “selected” class in my styles.css sheet?

  3. Ken-chan

    How can I edit this line:

    if ($(this).attr(“href”) == window.location.pathname){

    into something that may result to true as long as the pathname is found. I need an alternative for the == operators. Is there something in JS that emulates SQL’s “LIKE”?

    Thanks.

  4. Richard
    Permalink to comment#

    Thank you very much for this, there are hundreds of scripts about that do this , but this is the first one that actually works, does what it says on the tin. You are a genious, and have saved me a lot of pain, Thanks again!!!!!!!!!

  5. Ah, this is so difficult! I don’t understand any of it :(

    I guess I’ll just have to pay someone to keep working on my site.

  6. Rai
    Permalink to comment#

    Mmm, don´t sure why but is not working for me.
    I think that wordpress make this automatically adding .current to the currents pages.

  7. Permalink to comment#

    This looks to be the most succinct code I’ve come across for this. But I still can’t get it to work. I’m working on a Childtheme of Twenty Ten. If you have any tips on why it’s not working would love to hear it.

  8. Chris Bowyer

    Above code didn’t work for me, but this code I found did…

    
    $(function() {
    	$("#menu a").each(function() {
    		if (this.href == window.location) {
    			$(this).css("color", "#FF0");
    		};
    	});
    });
    
    
    • You can reference http://stackoverflow.com/questions/5874652/prop-vs-attr for more about prop vs attr.

    • Jesus
      Permalink to comment#

      This code worked for me as well.

    • Ubong
      Permalink to comment#

      Works like a charm> thank Chris

  9. for anyone having trouble implementing this, all you need to do is:

    1. place the code snipped inside in the head of your web document or external .js file

    2. create your selector class in your css file.
    .selected{color:#F00;} or whatever you like.

  10. scooter29
    Permalink to comment#

    Thanks Chris Bowyer.

    • scooter29
      Permalink to comment#

      I just mix both

      $(function() {
      $(“body a”).each(function() {
      if (this.href == window.location) {
      $(this).addClass(“selected”);
      };
      });
      });

  11. Permalink to comment#

    sometimes (if both classes have same attribute, this case “background”) you should make your css code with !important.

    for example; link with class somelink

    css code;

    a.somelink {
    background:white;
    }

    .selected {
    background:red !important;
    }

    with “!important” , red bg will pass the white bg. and when selected you will see red. if important not given you will see white bg, that case you think code not works ;)

Leave a Comment

Use markdown or basic HTML and be nice.