Thursday, April 12, 2012

Jquery take a price from a span and i want to make it static

i make this function :


function price(val){
 var test = $('span.hikashop_product_price_full:visible').text();
 var test2 = test.split('%');
 var test3 = test2[1].split(' ');
 var test4 = test3[0].replace(',','.');
 var eyy = parseFloat(test4) *val/100 + parseFloat(test4);
 eyy = eyy.toFixed(2)
 var ett = test.replace(test3[0]+' '+test3[1],'<span class=\"hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount\">'+eyy+' '+test3[1]+'</span>');
 $('span.hikashop_product_price_full:visible').html(ett);
 
 }
 


i take a value from a dynamic <span> tag (say it is dynamic because it changes depending on other field).
i have a field


<select id="custome_vara" name="data[item][custome_vara]" size="1" onchange="price(this.value);">
 <option value="" id="custome_vara_" selected="selected">alb</option>
 <option value="20" id="custome_vara_20">stejar auriu</option>
 <option value="30" id="custome_vara_30">moreiche</option>
 </select>
 


and it trigger every time i call it. I need to run just once to add % to the initial price.


the span looks like this


<span class="hikashop_product_discount">-10%</span><span class="hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount">51,34 €</span> </span> 
 


Question:


When I call the function price() it run the selection on this function and make the selection a int.
How to make the selection static and the field select/option add to this static selection a percent just once per option.


I hope u understood.


Sorry for my english.


Thanks.



Answer:

change
<select id="custome_vara" name="data[item][custome_vara]" size="1" onchange="price(this.value);">
to
<select id="custome_vara" name="data[item][custome_vara]" size="1">
then add this:
$('#custome_vara').one('change',function(){
    price($(this+'option:selected').val());
});
EDIT: In hopes of making your intent clear I will post some "errors" here:
First, you are missing markup example of:
'span.hikashop_product_price_full:visible'
Second:
eyy = eyy.toFixed(2)
should be:
eyy = eyy.toFixed(2);
Third:
'<span class=\"hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount\">'
can be written as:
'<span class="hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount">'
I created this fiddle page to help us understand each other: http://jsfiddle.net/2fxxD/
EDIT2: I think I see what you want. See the following in action here: http://jsfiddle.net/2fxxD/3/
function priceNew(val) {
    var discountedPrice = $('.hikashop_product_price_with_discount');
    var percentOff = $('.hikashop_product_discount');
    var price = discountedPrice.text().split(' ');
    var decimalPrice = price[0].replace(',', '.');
    var eyy = parseFloat(decimalPrice) * val / 100 + parseFloat(decimalPrice);
    eyy = eyy.toFixed(2);
    var newPrice = eyy + ' ' + price[1];
    discountedPrice.text(newPrice);
    percentOff.text('-' + val + '%');
    $('.hikashop_product_price').text(newPrice);
}

$('#custome_vara').one('change', function() {
    priceNew($(this + 'option:selected').val());
});
Markup:
<select id="custome_vara" name="data[item][custome_vara]" size="1">
    <option value="" id="custome_vara_" selected="selected">alb</option> 
    <option value="20" id="custome_vara_20">stejar auriu</option>
    <option value="30" id="custome_vara_30">moreiche</option>
</select>
<div>
<span class="hikashop_product_price_full" id="hikashop_product_price_full"> 
    <span class="hikashop_product_discount">-10%</span>
    <span class="hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount">51,34 €</span> 
    </span>
</div>


No comments:

Post a Comment