I have two divs created from PHP loop:
<div class="sku">
<span class="sku-value" data-id="150">1000</span>
<span class="sku-value" data-id="151">2000</span>
<span class="sku-value" data-id="152">3000</span>
</div>
<div class="size-values">
<span class="size-value" data-id="150">M</span>
<span class="size-value" data-id="151">L</span>
<span class="size-value" data-id="152">XL</span>
</div>
These divs has parent div called attributes.
sku-value and size-value have something in common: data-id attribute.
With CSS I'm manipulating the sku-value spans:
.sku-value {
display:none;
}
.sku-value.active {
display:inline-block;
}
With jQuery I'm displaying only the first size-value:
$('.sku-value:first').addClass('active');
So in this case when the page is loaded only this sku-value will be visible:
<span class="sku-value" data-id="150">1000</span>
With my code bellow I can successfully change the active class of each clicked size-value but how can I change the displayed sku-value that matches the data-id value?
For example: if a user click on one of size-value that has data-id 152, how can I display the sku-value that has data-id 152 and hide the current visible sku-value?
$(document).ready(function(){
$('.sku-value:first').addClass('active');
$('.attributes').find('.size-value').on('click', function(){
if ($this.hasClass('active')) {
$this.removeClass('active');
} else {
$this.closest('.size-values').find('.size-value').removeClass('active');
$this.addClass('active');
}
});
});