Member Avatar for nblackburn

Hello Developers,

i have a link structure like this on my website...

index.php - homepage
index.php?section=  - other pages

here is a piece of code that i wrote to change the colour of the active link...

$(function(){
     var path = location.pathname.substring(1);
     if ( path )
          $('#nav ul#navigation li a[href$="' + path + '"]').attr('class', 'selected');
});

but this doesn't take into account of php link structures, only flat file links. How would i get this working for links like "index.php?section=" aswell as the basic links like "index.php"?.

Kind Regards,
Nathaniel Blackburn

Recommended Answers

All 2 Replies

Use location.search to get the query from the url.

Like this:

$(function(){
     var path = location.pathname.substring(1);
     if ( location.search )
          path += location.search;
     if ( path )
          $('#nav ul#navigation li a[href$="' + path + '"]').attr('class', 'selected');
});

Hope it helps.

Member Avatar for nblackburn

Awesome thanks ill be sure to give this a go shortly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.