javascript - Adding input type values -


i'm trying add specific input id value that's inside of li tag. gets first number. don't know how second , third work. new javascript. heres how code looks like.

html

<li id="pallet1" class="inactive"> <span class="itemnumber">1</span> <input type="text" name="weight" placeholder="pallet weight (lbs)" id="qt" class='weight'/> </li>  <li id="pallet1" class="inactive"> <span class="itemnumber">2</span> <input type="text" name="weight" placeholder="pallet weight (lbs)" id="f"/> </li>  <li id="pallet1" class="inactive"> <span class="itemnumber">3</span> <input type="text" name="weight" placeholder="pallet weight (lbs)" id="f2"/> </li>  <span id="total"></span> 

javascript

$(".weight").keyup(function(){      var totalweight = $(".weight").val(); var totalweight2 = $("#f").val(); var totalweight3 = $("#f2").val();  if(totalweight2 == "" && totalweight3 == ""){     var total = totalweight; } if(totalweight3 == ""){     var total = number(totalweight)+number(totalweight2); } //this must add if(totalweight != "" && totalweight2 != "" && totalweight3 != ""){     var total = number(totalweight)+number(totalweight2)+number(totalweight3); }  $("#total").text(total); if(total == ""){    $("#total").text("0"); } }); 

here fiddle demo. if user press alphabet, don't need worry that. script calculate if input number.

you can use html markup below,

<li id="pallet1" class="inactive"> <span class="itemnumber">1</span>      <input type="text" name="weight" placeholder="pallet weight (lbs)" id="qt" class='weight' /> </li> <li id="pallet1" class="inactive"> <span class="itemnumber">2</span>      <input type="text" name="weight" placeholder="pallet weight (lbs)" id="f" class='weight' /> </li> <li id="pallet1" class="inactive"> <span class="itemnumber">3</span>      <input type="text" name="weight" placeholder="pallet weight (lbs)" id="f2" class='weight' /> </li> <span id="total"></span> 

you can use javascript code this,

$(".weight").keyup(function () {     var sum = 0;     $('.weight').each(function () {         if (!isnan(this.value) && this.value.length != 0) {             sum += +this.value;         }     });     $('#total').text(sum); }); 

see fiddle demo


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -