I have this jquery code
var theTotal = jQuery('.woocommerce-Price-amount.amount').text(); var price = theTotal.replace("$", ""); jQuery('input[name="dimming"]').bind('click', function (e) { var haller = jQuery(this).val(); if (haller == '1-10V dimmer') { price = Number(price) + 150; } else { price = Number(price) - 150; } jQuery('.woocommerce-Price-amount.amount').text(price); });
I have this radio button, so when I select radio button “layer2” it should add 150 to the original amount
[] layer1 (+ $0) [] layer2 (+ $150)
Result
<span class="woocommerce-Price-amount amount">746</span>
but when I click on layer2 multiple times it always adds up, it should always be original price + 150 no matter how many times I select that button, any help please
HERE IS MY FIDDLE https://jsfiddle.net/kxvc6t2L/
Advertisement
Answer
You can save the original Price into a variable and then you just need to add on the layer1 150 to the price. So you will always add from your originalPrice 150 to it. This way you won’t change that price and there is no way you will get more.
var theTotal = jQuery( '.woocommerce-Price-amount.amount' ).text(); var originalPrice = theTotal.replace( "$", "" ); jQuery( 'input[name="dimming"]' ).bind( 'click', function (e) { var haller = jQuery(this).val(); if ( haller == '1-10V dimmer' ) { price = Number( originalPrice ) + 150; } else { price = Number( originalPrice ); } jQuery( '.woocommerce-Price-amount.amount' ).text( price ); } );
Here is your working Fiddle: https://jsfiddle.net/bno4qsuk/