tags:

views:

49

answers:

2

I have a main navigation but I need to apply the class of 'current' to the <li> if it matches the URL I have specified it should.

This is what I have:

$(function() {

  var url = location.pathname;

  if(url.indexOf('girls')) {
    $("li#nav-01").addClass('current');
  }

  if(url.indexOf('boys')) {
    $("li#nav-02").addClass('current');
  }

  if(url.indexOf('capes')) {
    $("li#nav-03").addClass('current');
  }

  if(url.indexOf('about_us')) {
    $("li#nav-04").addClass('current');
  }

  if(url.indexOf('contact')) {
    $("li#nav-05").addClass('current');
  }

  if(url.indexOf('frequently_asked_questions')) {
    $("li#nav-06").addClass('current');
  }
});

and then

<div id="nav-main"> 
<ul class="navmain"> 
<li id="nav-01" class=""><a href="../girls/">Girls</a></li> 
<li id="nav-02" class=""><a href="../boys/">Boys</a></li> 
<li id="nav-03" class=""><a href="../capes/">Accessories</a></li>
...
</ul> 
</div>

This doesn't seem to work so What I am looking for is something that allows me to put the full URL up to a certain directory.

So where I have.

'if(url.indexOf...'

I'd like to have something along the lines of:

'if(url=http://www.siteaddress/directory/...'

So then if the current URL is '.../directoryname/' then the <li> I have specified should be selected ('current') depending on that URL, is.

Thanks for reading.

A: 

I'd probably place the potential directory matches in an Array, then look for the index of the directory in the array, and use that index number to specify which ID you want to use.

Because the index will be 0 based, I've added 1 to the index to give you a 1 based index like your navs.

$(function() {

  var arr = ['girls','boys','capes','about_us','contact','frequently_asked_questions'];
  var dir = location.pathname.split('/')[1];
  var index = $.inArray( dir, arr ) + 1;
  $("#nav-0" + index).addClass('current');

});
  • arr stores the directory names
  • dir is the pathname split on the / character. The item at index [1] should be the first directory.
  • index stores the result of the $.inArray method, which iterates through the arr Array looking for a match to dir, and returns the index number. (Again 1 is added to match your indexing.)
  • Then the index is concatenated into the selector, to select the proper one.

EDIT: Changed it to not use a regex for the split. Shouldn't be necessary.

patrick dw
Thanks for the quick reply I've been trying out the code but it doesn't seem to be apply the class to the navs, Here is what I have:$(function() { var arr = ['girls','boys','capes','about_us','contact','frequently_asked_questions']; var dir = location.pathname.split(/\//)[1]; var index = $.inArray( dir, arr ) + 1; $(".navmain > #nav-0" + index).addClass('current');});and then:<div id="nav-main"> <ul class="navmain"> <li id="nav-01" class=""><a href="../girls/"></a></li>/">Girls Dresses</a></li> <li id="nav-02"..</ul> </div>
Ok quick comments don't like formatting I've added the code to the first post.
@user - It seems to work for me. [Here's an example.](http://jsbin.com/abixe4/2/) The only thing I changed was that I gave `dir` a static value of "girls". Try `alert(dir)` to see what value you're getting.
patrick dw
Is there the ability to apply a class as if it were "hard coded" in the first place. What it seems to be doing is removing anything that was there in the first place and giving the nav-## a class of only 'current'. where current is a js class and not a css class.
@user - Any class added using `.addClass()` will append a new class to the element that will have the same effect as if you did it yourself in the HTML. As such, any styles for that class defined in your CSS will apply themselves as soon as the class is added. There isn't any such thing as a *js class* (at least not in the manner we're discussing).
patrick dw
Yea sorry I don't mean a js exclusive class what I mean is the class is being called from js and not css. also what i ment is if i go into view source the class isn't in the source as php might apply it.
@user - Modifications to the DOM do not typically appear in the "View Source" option that browsers give. But many browsers do ship with developer tools that will show modifications made via javascript. My point is that when you add a class via `.addClass()` (or whatever other means), the element should behave exactly as though it was added originally in your php. If it doesn't then there's some other issue.
patrick dw
@downvoter - Be decent. Give a reason why. What is wrong with the solution? It can be confusing to the OP when you down-vote correct solutions. If it is wrong, explain how.
patrick dw
A: 

Why even use an array?

using the same dir var from patrick dw's code...

  var dir = location.pathname.split('/')[1];
  $('ul.navmain').find('a[href*="' + dir + '"]').parent().addClass('current');
mike
You should really post more of an answer than just a link to docs. How would you use that selector for this issue?
patrick dw
I posted the wrong link anyway...
mike
The benefit to the array (since you asked) is that the index allows direct selection by ID, which will be faster than selecting an ancestor by class and then having to test the `href` attribute of descendant elements. You could also have false positives with an [attribute-contains](http://api.jquery.com/attribute-contains-selector/) selector.
patrick dw