Ilja
Ilja

Reputation: 46479

On click add class to element with certain class and remove it from all other elements with same class

Basically I am trying to figure out the following I have several elements like

<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>

I need to figure out how to add class to an element when it is clicked, so for example add .v-current , but at the same time remove it from all other elements with a class of .v-bar ? All with jQuery.

Upvotes: 1

Views: 1258

Answers (2)

Christoffer Mansfield
Christoffer Mansfield

Reputation: 803

$('.v-bar').click(function(){
    $('.v-current').removeClass('v-current');
    $(this).addClass('v-current');
});

Upvotes: 1

Andrei
Andrei

Reputation: 56688

$('.v-bar').click(function(e){
    $('.v-bar').removeClass('v-current');
    $(this).addClass('v-current');
    e.preventDefault();
});

Upvotes: 3

Related Questions