Reputation:
I have two spans with links that a user can click, and when a span is clicked it remove a class and adds another class on the click one:
HTML:
<span class="resCheck label label-success">
<a data-method="get" class="resCheckLink" href="trafikskola?utf8=%E2%9C%93&query=lund&
amp;sort=asc">Cheap prices</a>
</span>
<span class="resCheck label">
<a class="resCheckLink" data-method="get" href="trafikskola?utf8=%E2%9C%93&query=lund&
sort=desc">Expensive prices</a>
</span>
JQuery:
$(function(){
$('.resCheckLink').click(function(e){
$('.resCheck.label').removeClass('label-success');
$(this).parent().addClass('label-success')
});
});
But it dosent work, it dosent add and remove the classes, how can I fix this?
Upvotes: 0
Views: 7656
Reputation: 5775
You are removing and adding same class, so you will not be able to monitor actually,
$('.resCheck.label').removeClass('label-success');
$(this).parent().addClass('label-success');
so Please create another class with different style and then try, with answer given by PSR above.
that will do :)
Upvotes: 0
Reputation: 40318
try this
$(function(){
$('.resCheckLink').click(function(e){
$('.resCheck').removeClass('label-success');
$(this).parent().addClass('label-success');
});
});
Upvotes: 1
Reputation: 1418
Try this...
$(function(){
$('.resCheckLink').click(function(e){
$('.resCheck').removeClass('label-success');
$(this).parent().addClass('label-success');
});
});
Upvotes: 1
Reputation: 169
Try this
$(function(){
$('.resCheckLink').click(function(e){
$('.resCheck').removeClass('label-success');
$(this).parent().addClass('label-success')
});
});
Upvotes: 0
Reputation: 50149
It does work actually, I imagine you're styling the element incorrectly or something? I added return false
to prevent the link functioning.
$(function(){
$('.resCheckLink').click(function(e){
$('.resCheck.label').removeClass('label-success');
$(this).parent().addClass('label-success');
return false;
});
});
Upvotes: 0
Reputation: 8346
Try this.
$(function(){
$('.resCheckLink').click(function(e){
$('span.resCheck').removeClass('label-success');
$(this).parent().addClass('label-success')
});
});
Upvotes: 0