javascript - Calculating input fields with data attribute -


i have input fields data attribute.i want when click on button in field total sum of total of fields. found total sum every field, how sum of them.

<input type="text" id="total"> <div class="field">   <input type="number" min="1" max="255" data-price="2" class="wert"> </div> <div class="field">   <input type="number" min="1" max="255" data-price="2" class="wert"> </div> <button id="calc-butt">sum</button> <script>  jquery('#calc-butt').click(function() {     jquery('.field').each(function(){       var price = jquery(this).find('.wert').attr("data-price");       var cubic = jquery(this).find('.wert').val();       var total = price * cubic;      });  }); </script> 

scope of total inside each callback, intialize 0 before each ,check snippet

jquery('#calc-butt').click(function() {      var total = 0;      jquery('.field').each(function(){        var price = jquery(this).find('.wert').attr("data-price");        var cubic = jquery(this).find('.wert').val();        total += price * cubic;        jquery('#total').val(total);      });   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" id="total">  <div class="field">    <input type="number" min="1" max="255" data-price="2" class="wert">  </div>  <div class="field">    <input type="number" min="1" max="255" data-price="2" class="wert">  </div>  <button id="calc-butt">sum</button>


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -