I'm using jQuery and this is my code for the click event of any labels.
$('.spin span:last-child').click(function(){
unitPrice = parseFloat($(this).closest('.product').find('.unit-price span').text().substring(3));
if ( $(this).parent().find($('.custom-input')).val() == "" ) {
$(this).parent().find($('.custom-input')).val(1);
inputValue = parseInt($(this).parent().find($('.custom-input')).val());
subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
$(this).closest('.product').find('.total-price span').text(subTotal);
} else if ( inputValue =! "" ) {
inputValue = parseInt($(this).parent().find($('.custom-input')).val());
inputValue += 1
$(this).parent().find($('.custom-input')).val(inputValue);
subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
$(this).closest('.product').find('.total-price span').text(subTotal);
};
});
so , i created a function to optimize the code:
function getValues(){
unitPrice = parseFloat($(this).closest('.product').find('.unit-price span').text().substring(3));
subTotal = parseFloat(inputValue * unitPrice).toFixed(2);
return subTotal;
$(this).closest('.product').find('.total-price span').text(subTotal);
};
and my new block code should be look like this:
$('.spin span:last-child').click(function(){
if ( $(this).parent().find($('.custom-input')).val() == "" ) {
$(this).parent().find($('.custom-input')).val(1);
inputValue = parseInt($(this).parent().find($('.custom-input')).val());
getValues();
} else if ( inputValue =! "" ) {
inputValue = parseInt($(this).parent().find($('.custom-input')).val());
inputValue += 1
$(this).parent().find($('.custom-input')).val(inputValue);
getValues();
};
});
but my function "getValues" don't work , the selector "$(this).find..." inside a function should be the problem i think, can you guys help to fix that?
Thanks