views:

37

answers:

1

Hi guys needing a bit of help.

I am creating a one page personal site.

Each section has a menu in it to jump to another section, however i want to have a class added to menu for the current section:

i.e. if you are in about the about link would have a class 'current'.

This is how it looks.

<section id="about">
<nav>
    <li><a href="#" id ="about">About</a></li>
    <li><a href="#" id ="contact">About</a></li>
    <li><a href="#" id ="blog">About</a></li>
</nav>

New to jquery so i am struggling to find out how to do this.

Any help will be greatly appreciated.

Thanks

+1  A: 

First, you should give the section a matching class, or a prefixed ID (anything but the same ID), like this:

<section id="current-about">

IDs have to be unique, so they should not match directly. After doing the above, then you can do this:

$(function() {
  var id = $('section:has(nav)').attr('id').replace('current-','');
  $('#' + id).addClass('current');
});

What this does is gets the ID from the <section> containing a <nav>, strips off the prefix, then finds the element with that ID and adds the class. You can see/play with a demo of the code here.

Nick Craver
HiThanks for your reply.What i need to do though is to check what section i am in and then find if the nav list contains a link to that section and add the class onto the actual <li> not the container.This way i can highlight it as being current.Any ideas?Thanks A lot!
Robert Lawson
@Robert Lawson - That's what the above answer does (adding it to the `<li>`, see the demo)...I'm not sure how you would find what section you're in, do you mean the `id` on the `<section>` is *not* generated server-side?
Nick Craver
SorryI am having the menu multiple times on the page.I need the hrefs to make the page scroll to each section on the page which have that idif i had this<section id ="section-about"> <ul> <li><a href="#section-about" class="about">About</a></li> <li><a href="#section-contact" class="contact">Contact</a></li> <li><a href="#section-blog" class="blog">Blog</a></li> </ul></section>how would i achieve adding a current class to the about link?and the same for the others.
Robert Lawson
@Robert - I'm confused a bit, in the example you give, you're scrolling to the parent element (so, nowhere really), did you mean for the `<section>` to be in a *different* spot in the page? To do the literal example in your comment, it's like this: http://jsfiddle.net/N7VuT/ or by `href`, like this: http://jsfiddle.net/TT9Ct/ (even if I can't discern a useful application :)
Nick Craver
@Felix Kling Tried that here still cant get it.http://jsfiddle.net/x4x48/Thanks a lot for your help guys must be something silly im not doing?Cheers
Robert Lawson
@Robert - Are you using IE to test? Because `<section>` is an HTML5 element, and it's not supported there, not until IE9 anyway.
Nick Craver
@Nick Carver - Got yours href test to to work :) thanks one last thinghow would i get this to run threw each section i have and do the same thing.Just now it is only putting the first repetition onto each section<section id="section-blog"> - apply current to inner blog link<section id="section-contact"> - apply current to inner contact linkthanks again!
Robert Lawson
@Robert - Both examples will work...just remove the `:has(nav)` portion, since it doesn't support those tags. To do each section, change it a bit like this: http://jsfiddle.net/x4x48/2/ But I stress **you cannot reuse the same ID**, you'll get all sorts of odd behavior, as it's completely invalid HTML.
Nick Craver
@Nick Carver Verrry last time can you check this?http://jsfiddle.net/x4x48/2/want to use the href method but struggling again.Thanks so much for your help Nick
Robert Lawson
@Robert - Welcome :) here ya go: http://jsfiddle.net/x4x48/3/
Nick Craver
@Nick Carver - When i add a new section http://jsfiddle.net/x4x48/3/ it seems to highlight two in each section it should only highlight the one it belongs too i.e. if your in about then the about link will be highlighted, if your in blog the blog link should be highlightedcheers
Robert Lawson
@Nick Carver - Got it working!$(function() { $('section').each(function() { var href = this.id.replace('current-',''); $(this).find("a[href='#" + href + "']").addClass('current'); });});I can now eat!Thanks for all your help Nick
Robert Lawson
@Robert - Awesome good to hear :) Be sure to accept answers via the checkmark on the left if that solved it for you :)
Nick Craver