Reputation: 127
When clicking on one element within a parent div, id like to change that elements class, whilst also changing all other elements within the same parent. So far my code looks like this
$(".vehicle_block").unbind().click(function(e){
e.stopImmediatePropagation();
var booking_id = $("#booking_id").val();
var vehicle_no = $(this).parent().attr('id');
var vehicle_id = $(this).attr('name');
$(this).attr('class','vehicle_block selected');
});
So when a .vehicle_block element is clicked add the selected class. If the user was to then click a different .vehicle_block within the same parent I'd like to remove the previous select class and add it to the new element clicked.
Does anyone know how I would do this?
Upvotes: 0
Views: 116
Reputation: 1300
With https://api.jquery.com/siblings/ is one way
$(".vehicle_block").unbind().click(function(e){
e.stopImmediatePropagation();
var booking_id = $("#booking_id").val();
var vehicle_no = $(this).parent().attr('id');
var vehicle_id = $(this).attr('name');
$(this).addClass('selected');
$(this).siblings().removeClass('selected');
});
Upvotes: 3