views:

104

answers:

4

Hi!

I have a menu:

 <ul>
    <li id="active"><a href="/">Головна</a></li>
     <li><a href="/about/">Про компанію</a></li>
     <li><a href="/work/">Діяльність</a></li>
     <li><a href="/initiative/">Ініціативи</a></li>
     <li><a href="/statti/">Статті</a></li>
 </ul>

Can you suggest a way to change id=active attribute for this list. If for example I clicked on about, I want this li element to be marked as active.

Thanks

+2  A: 

Use class, not id. The reason is that id is for identifying elements, which in this case would mean that the only element in the page identified by "active" would be this one, even if you have other contexts needing an "actiuve" distinction. Using class lets you have several "active" elements in different context, and is more semantically correct.

streetpc
A: 

To be correct, you should use class, not id to do this. (See http://css-discuss.incutio.com/?page=ClassesVsIds for more explanation. Basically ids should be unique to the page.)

You can modify the class using jQuery's built in attribute modifying functions: http://docs.jquery.com/Attributes

Look under "Class"

Marquis Wang
A: 

If your menu had an id, let's say "xpto", the onclick of each link could trigger a function like this:

onclick="setActiveId(this);"


functionSetActiveId(newActiveElem){
  var allElems = document.getElementById('xpto').childNodes;

  for(var i = 0; i < allElems.length; i++){
    allElems[i].id = '';
  }

  newActiveElem.id = 'active';
}
ajdramos
My solution seems to be almost exactly as the one posted a couple of seconds before. I agree that using a class tag would be semantically more correct.
ajdramos
+2  A: 

There is an elegant css method of styling the currently active navigation state without any javascript or server side support. Basically one id (e.g. as body id attribute) that uniquely identifies the navigation state is enough to create css selectors that highlight the active state.

Josef
That is a really cool technique! +1.
Jacob