how can I set a default value to zero if nothing is entered into the text field?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luke noob
    New Member
    • Aug 2009
    • 69

    how can I set a default value to zero if nothing is entered into the text field?

    my problem is that when i dont type anything into the quantity textbox my jquery function calc() does not work. this is because '' = NAN (not a number)

    I have tried add this code in my calc() function on cart_actions.js , but with no luck...
    [code=jquery]
    if(quantity.len gth === 0){
    quantity = $(".quantity input", this).val(0);

    }
    [/code]

    all it does is set the value to zero on the textbox, but it does recognize it as a zero in the script, the prices return NAN (AddedQuantity AND totalPrice). I have been scratching my head for weeks please help :) i have done an alert(quantity) aswell and that came back zero, but still AddedQuantity AND totalPrice say NAN.

    showcart.php
    [code=php]

    <input type='text' name='quantity[$id]' size='3' value='$product Qty' /><br /></td>

    [/code]

    cart_actions.js

    [code=jquery]
    $(function() {

    $("#cart tr .quantity input").change( function() {
    var id = $(this).attr("n ame").slice(9, -1);
    var quantity = $(this).val();


    $.ajax({
    type: "GET",
    url: "cart_action.ph p",
    data: "quantity[" + id + "]=" + quantity,
    success: function() {
    var startColor = $("#cart tr .quantity input[name*=" + id + "]").parent().par ent().hasClass( "odd") ? "#eeeeee" : "#ffffff";
    $("#cart tr .quantity input[name*=" + id + "]").parent().par ent().find("td" ).animate({ backgroundColor : "#ff8" }, 100).animate({ backgroundColor : startColor }, 800);
    calcPrice();
    },
    error: function() {
    window.location ("cart_action.p hp?quantity[" + id + "]=" + quantity);
    }
    });
    });


    function calcPrice() {

    var totalPrice = 0;
    var AddedQuantity = 0;


    $("#cart tr .quantity").par ent().each(func tion() {
    var quantity = $(".quantity input", this).val();
    if(quantity.len gth === 0){
    quantity = $(".quantity input", this).val(0);
    }

    var unitPrice = $(".unit_price" , this).text().sl ice(1);

    var extendedPrice = quantity*unitPr ice;

    var extendedPriceZe ros = extendedPrice.t oFixed(2);



    totalPrice += extendedPrice;
    AddedQuantity += parseInt(quanti ty);

    $(".extended_pr ice", this).html("£" + extendedPrice.t oFixed(2));
    $("#total_price ").html("£" + totalPrice.toFi xed(2));
    $("#test").html (AddedQuantity) ;


    });
    if ( totalPrice == 0 ) {
    $("#cart").pare nt().replaceWit h("<div id='container'> <h1>Shopping Cart</h1><p>You have no items in your cart. Please <a href='home.php' >Continue to shop</a>!</p></div>");
    }
    if ( AddedQuantity >= 10 ) {
    $("#cart td#shipping").h tml("Free !");
    }
    if ( AddedQuantity < 10 ) {
    $("#cart td#shipping").h tml("£"+AddedQu antity*5);
    }


    }
    });

    [/code]
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    hi
    why dont you put 0 in the value attribute of the quantity by default ,and then use this method,
    Code:
    <input type="text" onblur='if(this.value==""){this.value=0;}'>
    , i hope i got your problem right.


    apart in your existing code , add parseInt() to the line number 38 where you are multiplying the quantity with price.
    Code:
    var extendedPrice = parseInt(quantity)*parseInt(unitPrice);
    regards,
    Omer Aslam

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      the problem is line 33. if you re-set quantity, it becomes a JQuery object (while $(…).val() is a number), which on line 38 will produce NaN.

      for the calculation itself it would suffice to set quantity to 0.

      @Omer: parseInt(JQuery ) => NaN

      Comment

      • luke noob
        New Member
        • Aug 2009
        • 69

        #4
        thank you i have added onblur='if(this .value==""){thi s.value=0;}' and got rid of line

        Code:
        if(quantity.length === 0){
                  quantity = $(".quantity input", this).val(0); 
                  }
        it now seems to work, is there anything else im missing or should that be ok?

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          hi luke ,
          look at Dormilich last post, what he said is to be considered, but if you have removed that line its fine i think, and please select the correct answer which resolved your conflict so that any other person with the same issue who comes googling around could have help too.
          regards,
          Omer Aslam

          Comment

          Working...