views:

182

answers:

4

I'm no javascript master, but I need to create a script that accomploshies the following.

If body id="index" add class"current-selected" to anchor tag where href=index.php If body id="services" add class="current-selected" to to anchor tag where href=services.php etc.

Does this make any sense? Can anyone help?

Thanks if you can!

Abbotsford Web Design

+1  A: 

Assuming your body ID will not always match up perfectly with the file-name of its corresponding link, you can do it this way.

$("body#index a[href='index.php']").addClass("current-selected");
$("body#service a[href='service.php']").addClass("current-selected");

If your body ID will always match your corresponding link href value, you can extract the body ID first, and use it as a variable within the jQuery selector:

var body_id = $("body").attr("id");
$("a[href='"+body_id+".php']").addClass("current-selected");
Jonathan Sampson
and the reverse being `.removeClass("current-selected")` (since it's in the question title)
Kevin Peno
I've added this script to the header, I will seperate it to a seperate file later, but without any success. The link to the site I'm working with is http://fraservalley-webdesign.com/index.php.Any thoughts?
Ben Stewart
Ben, I just ran the first example against your site and it worked just fine.
Jonathan Sampson
I also tried the second, and it too worked as expected. I would suggest you re-examine where you pasted the code in your project and try again - it should work.
Jonathan Sampson
I must be thick, sorry. Do you have an example I can look at?
Ben Stewart
Ben, using firebug in firefox, I just copied and pasted the code from above directly into the console, and it resulted in the `current-selected` class being added to the index link.
Jonathan Sampson
Thanks for the help. I found my mistakes!
Ben Stewart
Good work, Ben!
Jonathan Sampson
+4  A: 

Something like this should do the trick. This will dynamically determine your body element ID and change all anchor tags that reference the php file of the same name.

var bodyID = $("body").attr("id");

$("a[href='" + bodyID + ".php']").addClass("current-selected");


As noted in another comment, use the .removeClass("current-selected") function to accomplish the reverse.

Also, if your URL's only end with "index.php", i.e. the href attribute is something like "/something/index.php", use "a[href$='" + bodyID + ".php']" as your selector. That will match href values that end with the file name.

womp
+1 Nice and DRY.
micahwittman
The asker put "etc." in his question so I'm assuming he has more than just the two body ID examples.
womp
+1 for exactly what I would've said
darkporter
A: 
 $('body#index a[href="index.php"]').addClass("current-selected");
 $('body#services a[href="services.php"]').addClass("current-selected");
antpaw
+1  A: 

Building on http://stackoverflow.com/questions/1924723/using-jquery-to-add-remove-a-class-based-on-body-id/1924754#1924754:

var bodyID = $('body').attr('id');

$("a[href$='" + bodyID + ".php']").toggleClass('current-selected'); //add/remove

OR

$("a[href$='" + bodyID + ".php']").addClass('current-selected'); //add

Instead of "=", we use "$=" (referring to "href$=") syntax which will matched the end of the string, so both "index.php" and "/index.php" will be matched by "index.php".

To implement it on your site, you need to run the above code inside the jQuery ready function so all the HTML below the script block loads before the Javascript performs actions on it:

EDIT: This works for all main/top navigation links for your site (string for matching the href is the last path segment of the URL):

<script type="text/javascript">
$(document).ready(function(){
    page = window.location.pathname.substring(1).replace(/\//g,'');
    $("a[href*='" + page + "']").addClass('current-selected');
});
</script>
micahwittman
I no nothing of Java so thanks for your help. I've added this script to the header at this page http://fraservalley-webdesign.com/index.php. I've the body id and the href line up, and yet it isn't working. Do you have any ideas?
Ben Stewart
*looking right now...*
micahwittman
Thanks. I've been buggering around with it, still no luck.
Ben Stewart
Ok, I see now. Your footer links work: their links have no leading slash, just "index.php". The header links are like "/index.php". The solution in my answer above ($("a[href$='" + bodyID + ".php']").toggleClass('current-selected');)
micahwittman
Thanks for the help, but i still can't seem to make this work. I've copied and pasted your code exactly, but no luck.
Ben Stewart
*checking your live page...*
micahwittman
The code was firing before the HTML loads. See the bottom of my Answer for the replacement code.
micahwittman
EUREKA! Thanks so much, you've saved my life (or so it feels). By the way, great name. My son's name is Micah
Ben Stewart
Ben, glad to help. I lived half my life in Langley, though I'm not there now. Give my regards to everyone in the Fraser Valley. :)
micahwittman
And you son has one fantastic name, btw.
micahwittman
Wow. Small world. I do have on last question, which I've found a long work around for, but if you have a few minutes to help I would be eternally greatful! In the script provided the menu item must end in .php for it to work. Is there away to make the ending a variable like you did the beginning when you added $ to the href=?
Ben Stewart
Ben, when I looked closer, you had more than one kind of URL pattern happening - some with .php, some just a directory. So here's a better approach that works for all main/top navigation links (SEE bottom of answer again).
micahwittman