2

The script that I use currently works well actually, as long as I keep my file structure like this:

http://website.com/
http://website.com/page1.php
http://website.com/page2.php
http://website.com/page3.php

but when I impliment my mod_rewrite code to take away the ".php"

http://website.com/
http://website.com/page1
http://website.com/page2
http://website.com/page3

my navigation highlight script stops working.

Here is how I implement my code

The HTML

<body id="page1">

The JS

$(function() {

    //highlight the current nav
    //each of the following lines represents a different page
    $("#page1 a:contains('Page 1')").parent().addClass('active');
    $("#page2 a:contains('Page 2')").parent().addClass('active');



});

The ID within the JS looks for the ID within each body tag, and the part that says ('Page 1') looks for the item within the menu to identify which menu item to highlight. As I said, it works fine, until take away the .php. I'm assuming it has something to do with .parent within the js, but I'm not sure what to replace it with, or how to make it work right. Any suggestions?

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
Aileen Bea
  • 35
  • 8

1 Answers1

1

If the <body> tag in each of your pages, contains an unique ID, you could just check if this id is existing.

Like this (UPDATED):

for(var i = 0; i < $(".menu-item").length; i++){
    var item = i+1;
    if($("#page"+item).length>0){
        $("li:nth-child("+item+")").addClass("active");
        break;
    }
}

UPDATE Your updated fiddle

Fiddle

Ramiz Wachtler
  • 5,623
  • 2
  • 28
  • 33
  • I apologies for my ignorance - am I suppose to replace this with the JS I already have - or am I suppose to somehow integrate it into the script? I tried replacing it with the script you provided, but I'm afraid it didn't work. Thank you so much for taking the time to help me! – Aileen Bea Jan 29 '15 at 07:37
  • It should replace your code. Could you also post the content of your page1.php or at least your navigation? – Ramiz Wachtler Jan 29 '15 at 07:40
  • I've also added a fiddle to my answer, check it out. – Ramiz Wachtler Jan 29 '15 at 07:52
  • The ID in the `` tag identifies the correct page, and the bit of code that says `a:contains('Page 1')` looks for the text within the `
  • ` tag to identify which menu item to highlight. does that make sense?
  • – Aileen Bea Jan 29 '15 at 07:55
  • It will add `.active` class to every matching element. Example: `// will match because there is an element "Home" on page1 $("#page1 a:contains('Home')").parent().addClass('active'); // will ALSO match because there is also an element "Rates" on page1 $("#page1 a:contains('Rates')").parent().addClass('active');` – Ramiz Wachtler Jan 29 '15 at 07:57