0

On my view_cart.php I have an overview of all the products to be ordered. Here I like to get the total quantity of products.
Every row represents a product where i have a field like

<input type="hidden" name="item_qty[0]" value="3" />

and for example

<input type="hidden" name="item_qty[1]" value="5" />
<input type="hidden" name="item_qty[2]" value="2" />

Now i like to get the total (here 10) to specify some shipping. I tried

$("input[name='item_qty[]']").each(function() { 
    test_qty += $(this).val();
});

3 Answers 3

1

You should define and set counter test_qty to 0, use selector name starting with [name^='item_qty'] and parse value attribute to integer with parseInt (for example, you can also use multiplication, like test_qty+=$(this).val()*1 or unary operator + like test_qty+=+$(this).val()).

var test_qty = 0
$("input[name^='item_qty']").each(function() { 
    test_qty +=parseInt($(this).val(), 10)  
})
console.log(test_qty)

JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

1

You are accessing the element the wrong way, please, try this one:

$("input[name^='item_qty']").each(function() { 
                test_qty +=$(this).val();
                console.log(test_qty);
            });

Comments

0

You could add a class="item" to each input and then:

$(document).ready(function(){
  var test_qty = 0;
  $(".item").each(function(){
    test_qty += parseInt($(this).val());
    console.log(test_qty);
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.