views:

284

answers:

4

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 :

<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 and how to fix it? Thanks :)

+1  A: 
$(document).ready(function() {
  $(".buttons a").click(function() {
    $(".buttons a").removeClass("selected");
    $(this).addClass("selected"); 
  });
});
alex
The only problem is that this may produce erratic results due to race conditions where you can't be sure addClass will be fired after RemoveClass.
Jud Stephenson
@Jud You are correct, however I have used this in production code and never had it clash with itself.
alex
Forgive me if I am wrong, but doesn't JavaScript's single threaded nature mean that it will run each line one at a time, invoking any extra code (like jQuery's internal methods) before moving onto the next line to execute?
alex
@alex, I wouldn't bet too much on it, but I implemented that exact function with varying results. But, if it works in this scenario, why fix it?
Jud Stephenson
@alex, your solution works, but unfortunately it doesn't give the flexibility to style the parent tag. Thanks
antosha
+1  A: 

I would change:

$(".buttons").children().toggleClass("selected").siblings().removeClass("selected");

to

$("this").addClass('selected').siblings().removeClass("selected");
Jud Stephenson
A: 

The original code failed because this syntax is invalid:

.children().("a")

The rest of the code also had some fundamental flaws. Try this instead:

$(function(){
  $(".buttons a").click(function(){
    $(this).parent().addClass('selected'). // <li>
      siblings().removeClass('selected');
  });
});

In this correction, the class selected is applied to an <li> -- not an <a> to give you better flexibility while writing CSS.

Ron DeVera
This works great, thanks :)
antosha
A: 

Greetings,

Followed the above, however I'm unable to get the end result. The li item which is currently selected has no background. On click i have anchor placed within the same html page (page url doesn't change)

I want to create something like this image example image

Thanks for you kind help.

Regards

enter code here The HTML

<link  rel="stylesheet" type="text/css" href="screen.css"/>

<script type="text/javascript">
$(document).ready(function() {
  $(".buttons a").click(function() {
    $(".buttons a").removeClass("selected");
    $(this).addClass("selected"); 
  });
});



</script>

 <div id="side-bar">
 <ul class="buttons">
    <li><a class="button" href="#tier1">Link1</a></li>
    <li><a class="button" href="#tier2">Link2</a></li>
    <li><a class="button" href="#tier3">Link3</a></li>
    <li><a class="button" href="#tier4">Link4</a></li>
  </ul>
  </div>
   <div id="content">
   <a name="tier1" /><h4>Tier 1</h4>
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id nibh nec augue condimentum feugiat vitae in arcu. Quisque condimentum, est sit amet dictum pulvinar, dui nulla laoreet dui, ut consequat est massa vel massa. Pellentesque ultricies pharetra convallis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p>
   <a name="tier2" /><h4>Tier 2</h4>
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id nibh nec augue condimentum feugiat vitae in arcu. Quisque condimentum, est sit amet dictum pulvinar, dui nulla laoreet dui, ut consequat est massa vel massa. Pellentesque ultricies pharetra convallis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent tristique diam dapibus ante pulvinar ut sagittis tellus ornare.</p><p>Lorem ipsum dolor sit amet, </p>
   <a name="tier3" /><h4>Tier 3</h4>
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id nibh nec augue condimentum feugiat vitae in arcu. Quisque condimentum, est sit amet dictum pulvinar, dui nulla laoreet dui, ut consequat est massa vel massa. Pellentesque ultricies pharetra convallis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent tristique diam dapibus ante pulvinar ut sagittis tellus ornare.</p><p>Lorem ipsum dolor sit amet, </p>
   <a name="tier4" /><h4>Tier 4</h4>
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id nibh nec augue condimentum feugiat vitae in arcu. Quisque condimentum, est sit amet dictum pulvinar, dui nulla laoreet dui, ut consequat est massa vel massa. Pellentesque ultricies pharetra convallis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent tristique diam dapibus</p>
   </div>

THE CSS
#side-bar{
 position:relative;
 top:0;
 left:0;
 float:left;
 width:200px;
 }
#content{
 position:relative;
 top:0;
 left:0;
 float:left;
 width:500px;
 margin:0px 20px;
 }
ul.buttons{
 list-style:none;
 background: #ff0;
 }
.button {
 display:block;
 border-bottom:1px dotted #ccc;
 padding:10px;
 }
a:link, a:visited{
 color:#000;
 }
a.selected:link{
 color:#000;
 text-decoration: none;
 background:#ff0000;
 }
a:hover{
 text-decoration: none;
 }
You are having the same problem as I had. Take a look at Ron DeVera's solution.
antosha