Reputation: 6099
I am trying to change a list item when the user clicks the selected class is changed to the clicked item. I have this code:
$(function() {
$("a.product-page li").click(function(){
$(this).parent("a").addClass("selected-page");
$("#options a.selected-page").removeClass("selected-page");
});
});
However the removeClass works however the addClass does not.
Here is the Site.
HTML:
<ul id="options">
<a href="#summary" id="summary" class="product-page selected-page"><span></span><li>Summary</li></a>
<a href="#specs" id="specs" class="product-page"><span></span><li>Specs</li></a>
<a href="#media" id="media" class="product-page"><span></span><li>Media</li></a>
<a href="#reviews" id="review" class="product-page"><span></span><li>Reviews</li></a>
</ul>
CSS:
ul#options {
margin-left:0px;
}
ul#options li{
list-style:none;
padding: 10px 20px;
background: #CCC;
border-top: 1px #999 solid;
border-left: 1px #999 solid;
border-right: 1px #999 solid;
}
ul#options a{
display:block;
border:none !important;
}
ul#options a.selected-page span{
position:absolute;
width:0px;
height:0px;
border-width:10px;
border-color:transparent #999999 transparent transparent;
border-style:solid;
margin-left:270px;
margin-top:11px;
}
ul#options a:last-child li {
border-bottom:1px #999 solid;
}
Upvotes: 0
Views: 147
Reputation: 3508
Reverse the lines
$(this).parent("a").addClass("selected-page");
$("#options a.selected-page").removeClass("selected-page");
Upvotes: 1
Reputation: 79830
<a>
link tag inside li
tag like below,HTML:
<ul id="options">
<li><a href="#summary" id="summary" class="product-page selected-page"><span></span>Summary</a></li>
<li><a href="#specs" id="specs" class="product-page"><span></span>Specs</a></li>
<li><a href="#media" id="media" class="product-page"><span></span>Media</a></li>
<li><a href="#reviews" id="review" class="product-page"><span></span>Reviews</a></li>
</ul>
Code:
$(function() {
$("a.product-page", $('#options')).click(function(){
$(this).addClass("selected-page");
$("#options a.selected-page").not(this).removeClass("selected-page");
});
});
Upvotes: 2
Reputation: 4568
You are adding the class and then removing in the next line.
The order here
$(this).parent("a").addClass("selected-page");
$("#options a.selected-page").removeClass("selected-page");
is incorrect.
Upvotes: 2
Reputation: 48761
It's because you're immediately removing the class. Flip those two lines around, and it'll work.
$(function() {
$("a.product-page li").click(function(){
$("#options a.selected-page").removeClass("selected-page");
$(this).parent("a").addClass("selected-page");
});
});
As noted in a comment above, your HTML is terribly malformed as well. That will need to be fixed.
Upvotes: 7