user818671
user818671

Reputation: 399

compare two values in textbox using jquery

i am working on following link: http://www.marvelfloors.com/ProductDetails.asp?ProductCode=0_OH501

i want to compare the values in texbox that is sqft textbox and the third textbox that has value 800. i have assigned the value 800 to the third text box using jquery.the value in third textbox will change dynamically if the value in span tag having class 'test' is changed.

i have compared the values of the two textboxes. but if i enter 1234 which is greater than 800 it does not go in if loop. but if i enter 801 it goes in if loop. please can you advise.

You can check the example at above link.

Below is the jquery code i have written:

// calculator sqft
$(function() {
    // prepare calc
    $('.colors_pricebox').after().append('<div id="calculator_sqft"><div class="calculator_sqft_t"><table border=0><tr><td colspan=4 class=titlecalc>Cartons estimator</td></tr><tr><td valign=top align=center class=sqfttt><input type=text name=sqft value="" /><br>sqft</td><td><img src="http://www.rtaflooring.com/v/vspfiles/templates/adorn/images/sqft-cartons-arrow.png" style="margin-top: -12px;"></td><td valign=top align=center class=cartonsttt><input type=text name=cartons value="" /><br>cartons</td><td valign=top class=xttt>x</td><td valign=top class=csqft_pricettt>$<span class=csqft_price></span></td><td valign=top class=xttt>=</td><td valign=top class=result_csqft></td></tr></table><input type=text name=sf value="" /></div></div>');
    // end prepare calc

    $('#calculator_sqft .result_csqft').html('$0.00');
    var sqft = $('#calculator_sqft input[name=sqft]').val();
    $('#calculator_sqft input[name=sqft]').live('keyup', function() {

        var qty = $('.test').html();

        $("#calculator_sqft input[name=sf]").val($('.test').html());

        var sqqty = $('#calculator_sqft input[name=sqft]').val();
        var test = $('#calculator_sqft input[name=sf]').val();

        alert($('#calculator_sqft input[name=sf]').val());
        if (sqqty > test) {
            alert("hi");
            $('#calculator_sqft .csqft_price').html($('.sqft_cart_price').html());

            var sqft = $('#calculator_sqft input[name=sqft]').val();
            var sqft_cart = $('.sqft_cart').html();
            var csqft_price = $('.csqft_price').html();
            var result_cart = Math.floor(sqft / sqft_cart);
            var final_price = result_cart * csqft_price;

            $('input[name=cartons]').val(result_cart);
            $('.result_csqft').html('$' + final_price.toFixed(2) + ' <span style="color: #f0b404; font: 14px arial;"></span>');
            $('input').eq(2).val(result_cart);
        }
        else {
            $('#calculator_sqft .csqft_price').html($('.proprice').html());
            //var sqft = $(this).val();
            var sqft = $('#calculator_sqft input[name=sqft]').val();
            var sqft_cart = $('.sqft_cart').html();
            var proprice = $('.proprice').html();
            var result_cart = Math.floor(sqft / sqft_cart);
            var final_price = result_cart * proprice;

            $('input[name=cartons]').val(result_cart);
            $('.result_csqft').html('$' + final_price.toFixed(2) + ' <span style="color: #f0b404; font: 14px arial;"></span>');
            $('input').eq(2).val(result_cart);

        }
    });
});
// end calculator

Upvotes: 2

Views: 3547

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

Have you tried converting the values to numbers

  var sqqty = parseInt($('#calculator_sqft input[name=sqft]').val(), 10);
  var test = parseInt($('#calculator_sqft input[name=sf]').val(), 10);

before comparing them?Because they are compared as string right now. in fact if you enter '9' you go int the if because 9 as a string is greater than 800

Upvotes: 1

Related Questions