Skip to content Skip to sidebar Skip to footer

Calculate One X Another In Jquery

I'm getting NaN. How can I do this and have it update every time time qty is changed? $('input[name='qtybox']').change(function(){ var price = parseInt($(this).text(), 10) * parseF

Solution 1:

Are you looking for something like this jsFiddle example?

jQuery:

$('input.qtybox').each(function() {
    $(this).keyup(function() {
        var price = $(this).parent().siblings('td#price').text();
        var qty = $(this).val();
        $(this).parent().siblings('td#tPrice').html(isNaN( ((price * qty).toFixed(2)) ) ? 0 : ((price * qty).toFixed(2)));
    })
})

Solution 2:

parseInt() evaluates to NaN. You're trying to parse an integer from an empty string.

Please provide a more complete code sample; I believe the problem is that you're trying to use closest() when you should be using find().

Solution 3:

A table cell doesn't have a value. Use the text method instead.

Also, parsing the string 2.79 as an integer will give you just 2.

parseFloat($(this).closest('#price').text())

Solution 4:

The problem is that .closest() looks up the chain. To make things less complicated, just don't specify a boundary:

$('#tPrice').each(function(){
    var price = parseInt($('input[name="qtybox"]').val(), 10 * parseInt($('#price').val(), 10);
    $(this).html(price);
});

To keep your current model:

$('#tPrice').each(function(){
    var price = parseInt($(this).prevAll('input[name="qtybox"]').val(), 10 * parseInt($(this).prevAll('#price').val(), 10);
    $(this).html(price);
});

Though you should really never constrain an ID selector unless you have a good reason to, because it can't be optimized if you do so.

Post a Comment for "Calculate One X Another In Jquery"