0
    <ul class="liste_couleur_qty">
         <li style="margin-bottom: 20px;">
                <dl>
                        <table width="200" border="0">
                      <tbody><tr>
                        <td width="50%">
                        <span style="display: block; font-size: 13px; line-height: 16px; margin-bottom: 2px;margin-right: 0px;margin-left: 20px;">Noir</span>
                    </td>

                        <td width="50%"><div class="add-to-cart">

                          <label for="qty-2195">qty :</label>

                          <input type="text" class="input-text qty calcul_qty_product" title="Qté" value="0" autocomplete="off" maxlength="5" data-product_color="127" id="qty-2195" name="qty-2195" onblur="addToCartPlus(2195, 127, this);">

                        </div></td>
                      </tr>
                    </tbody></table>
                    </dl>
            </li>

            <li style="margin-bottom: 20px;">
                <dl>
                        <table width="200" border="0">
                      <tbody><tr>
                        <td width="50%">
                        <span style="display: block; font-size: 13px; line-height: 16px; margin-bottom: 2px;margin-right: 0px;margin-left: 20px;">Blanc</span>
                    </td>

                        <td width="50%"><div class="add-to-cart">

                          <label for="qty-2196">qty :</label>

                          <input type="text" class="input-text qty calcul_qty_product" title="Qté" value="0" autocomplete="off" maxlength="5"  id="qty-2196" name="qty-2196" onblur="addToCartPlus();">

                        </div></td>
                      </tr>
                    </tbody></table>
                    </dl>
            </li>
<li style="margin-bottom: 20px;">
                <dl>
                        <table width="200" border="0">
                      <tbody><tr>
                        <td width="50%">
                        <span style="display: block; font-size: 13px; line-height: 16px; margin-bottom: 2px;margin-right: 0px;margin-left: 20px;">Blanc</span>
                    </td>

                        <td width="50%"><div class="add-to-cart">

                          <label for="qty-2196">qty :</label>

                          <input type="text" class="input-text qty calcul_qty_product" title="Qté" value="0" autocomplete="off" maxlength="5"  id="qty-2196" name="qty-2196" onblur="addToCartPlus();">

                        </div></td>
                      </tr>
                    </tbody></table>
                    </dl>
            </li>

                </ul> 

how to get all the input value and then add them all then give it to a variable. if in jquery,i know how to do.

var length = $('liste_couleur_qty li input').length;
var input = $('liste_couleur_qty li input').val;
for(var i=0;i< length;i++){
  var result += input;
}

the result will get all the value all.but my code still can't work.what's wrong with my jquery code? if i not want to use jquery only use javascript. how do i do?

0

4 Answers 4

2

This should work with jQuery:

var result = "";
$('liste_couleur_qty li input').each(function() {
   result += $(this).val();
});

First: jQuery.val is not a property, but a method. Second: it will return value of first element in result set, not all values. Third: in your for loop - I can't even guess what are you trying to do. You are always using the same value length times. Also, once you are trying to get numerical value, you should use parseInt/parseFloat methods as mentioned by undefined

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

1 Comment

You need to declare result outside of the function result = 0 otherwise result will concatenate the value "undefined" with the first $(this).val()
2

Your selector selects nothing, you have missed the . for the class selector, also val is a method not a property, you can use each method and parseInt function:

var result = 0;
$('.liste_couleur_qty li input').each(function(){
     result += parseInt(this.value, 10);
});

Comments

2
var result = 0;
$('.liste_couleur_qty li input').each(function(){
    result += $(this).val();
});
console.log(result);

This is not entirely accurate. As @undefined has pointed out, you need to use parseInt to get perform numeric addition instead of concatenation. That is:

result += parseInt($(this).val());

Comments

0
var length = $('liste_couleur_qty li input').length;
var input = $('liste_couleur_qty li input').val();
for(var i=0;i< length;i++){
  var result += input;
}

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.