I'm displaying variable prices, but I only want to display the price of a selected/choosen one.
My code as follows: Variable prices with input field inside a dropdown menu
<ul>
<li class="km-price-options" id="price_option_115_ssize">
<label>
<input type="radio" name="options[price_id][]" id="price_option_115_1" class="price_option_115" value="1" data-price="9.95">
<span>S Size</span>
</label>
</li>
<li class="km-price-options" id="price_option_115_msize">
<label>
<input type="radio" checked="checked" name="options[price_id][]" id="price_option_115_2" class="price_option_115" value="2" data-price="19.25">
<span>M Size</span>
</label>
</li>
<li class="km-price-options" id="price_option_115_xsize">
<label>
<input type="radio" name="options[price_id][]" id="price_option_115_3" class="price_option_115" value="3" data-price="99.50">
<span>X Size</span>
</label>
</li>
...
</ul>
Elsewhere on the page the prices
<div class="km-item-price">
<div class="km-price km-price-option-1">
<div class="km-item-price">$9.95</div>
</div>
<div class="km-price km-price-option-2 checked">
<div class="km-item-price">$19.25</div>
</div>
<div class="km-price km-price-option-3">
<div class="km-item-price">$99.50</div>
</div>
...
</div>
By default option 2 is checked.
When e.g. option 1 is selected inside the dropdown menu, I want the checked class be removed from km-price-option-2 and added inside km-price-option-1. Same for the imput field checked="checked".
So far my jquery code is as follows:
$('input:radio').change(function(){
if($(this).is(":checked")) {
$('.km-price').addClass("checked");
} else {
$('.km-price').removeClass("checked");
}
});
This will add a checked class name on all divs with the name km-price.
But it should be limited to a value="1" (input). Is this possible with jquery? If yes, how? If there's a better way, I'm all ears. :)