jQuery menu active link [SOLVED]

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
antosha
Forum Members
Forum Members
Posts: 60
Joined: Fri Jun 06, 2008 7:20 pm

jQuery menu active link [SOLVED]

Post by antosha »

Hello,
I am trying to make a jquery menu that when I click on one of the links (without reloading the page), it changes its class to "active" and removes this class when I click on another link.

here is my code :

Code: Select all

<__script__ type="text/javascript">
$(document).ready(function() {
  $(".buttons").children().("a").click(function() {
    $(".buttons").children().toggleClass("selected").siblings().removeClass("selected");
  });
});
</__script>


  <ul class="buttons">
    <li><a class="button" href="#">Link1</a></li>
    <li><a class="button" href="#">Link2</a></li>
    <li><a class="button" href="#">Link3</a></li>
    <li><a class="button" href="#">Link4</a></li>
  </ul>
Can someone tell me why my code is not working?
Thanks
Last edited by antosha on Sat Mar 27, 2010 12:43 am, edited 1 time in total.
Follow me on twitter: end_tone
Linked In: levchenkoanton
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am

Re: jQuery menu active link

Post by nicmare »

make it much more easier:
$("ul.buttons a").click(function () {
$(this).toggleClass("selected");
});
antosha
Forum Members
Forum Members
Posts: 60
Joined: Fri Jun 06, 2008 7:20 pm

Re: jQuery menu active link

Post by antosha »

thanks for the reply, but that doesn't do what I want.
With your solution, if I click on another link, the link I clicked before keeps the "selected" class.

You need to remove the class on the other elements before.
I got it working like that:

Code: Select all

 $(document).ready(function() { 
 $("a.button").click(function () {
 $("a.button").removeClass("selected");
   $(this).toggleClass("selected");
 });
}); 
Thanks
Last edited by antosha on Fri Mar 26, 2010 11:19 pm, edited 1 time in total.
Follow me on twitter: end_tone
Linked In: levchenkoanton
Post Reply

Return to “Layout and Design (CSS & HTML)”