Link to home
Start Free TrialLog in
Avatar of Seven price
Seven priceFlag for United States of America

asked on

total value on client side.

Is there a way on the client side to total up the quantity on every row in real time. what I have below and show in a label. if not in jquery even Javascript.
 
$(window).load(function() {
     $('input[id^="txtquantity"]').live('onkeyup', function() {
         if (String($(this).val()).length < 1) return false;
         

            });
        });

  <label></label>
<input type="text" id="txtquantity1" value="0" />
    <input type="text" id="txtquantity2" value="0" />
    <input type="text" id="txtquantity3" value="0" />
    <input type="text" id="txtquantity4" value="0" />
    <input type="text" id="txtquantity5" value="0" />
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Seven price

ASKER

Ok I will give it a try.
keep getting error

    //<![CDATA[
     $(window).load(function() {
   
        $('input[id^="txtquantity"]').each(function() {
   var total=0,val;
$("input[name^='txtquantity').each(function() {
  val= $(this).val();
  total+=isNaN(val)?0:parseInt(val,10);
}
$("#result").val(total);
}
}

     
     //]]>
What error?
sorry my friend a still a student in jquery with the closing tags.

     $(window).load(function() {
   
        var total=0,val;
$('input[id^="txtquantity"]').each(function() {
  val= $(this).val();
  total+=isNaN(val)?0:parseInt(val,10);

}
$("#result").val(total);
});
ok i got the syntax but nothing

 //<![CDATA[
           $(window).load(function() {
               var total = 0, val;
               $('input[id^="txtquantity"]').each(function() {
                   val = $(this).val();
                   total += isNaN(val) ? 0 : parseInt(val, 10);
                   //  $("#result").val(total);
                   {
                       $("#result").val(total);
                   }
               });
           });
               
    //]]>


  <div id="results">dd</div>
Sorry I was reading this on a mobile - not so easy to answer.

Try this
$(document).ready(function() {
  var inputs = $('input[id^="txtquantity"]')
  inputs.live('onkeyup', function() {
    var total=0,val;
    inputs.each(function() {
      val=$(this).val();
      total+=isNaN(val)?0:parseInt(val,10);
    });
    $("#result").text(total);
    if (String($(this).val()).length < 1) return false;
  });
});

Open in new window

confused now. I do not see the results you can remove

 if (String($(this).val()).length < 1) return false;

i am just trying to get the total
You may get a scoping issue - I will go to bed now and if you need help I will try a jsfiddle.net tomorrow
ok sorry myself. but there is no error but I also do not see any results

<div id="results"></div>

why is that
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see but for some reason the edit is not working

test it here
http://jsfiddle.net/
mine looks similar to what is posted before :) with usage of isNaN and parseInt (copied from previous post) my solution is
<html>
<head>
			<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" ></script>
<script>
$(document).ready(function() {
//add functions here
	$('input[id^="txtquantity"]').live('keyup', function() {
		if (String($(this).val()).length < 1) return false;
		getSum();
	});
});
function getSum(){
	var s = 0;
	$('input[id^="txtquantity"]').each(function(){
		//alert($(this).val());
		var v = $(this).val();
		s=s+( isNaN(v) ? 0 : parseInt(v) );
	});
	$('#lblSum').html(s);
}
</script>
</head>

<body>

SUM : <label id=lblSum></label><br>
<input type="text" id="txtquantity1" value="0" />
<input type="text" id="txtquantity2" value="0" />
<input type="text" id="txtquantity3" value="0" />
<input type="text" id="txtquantity4" value="0" />
<input type="text" id="txtquantity5" value="0" />
    
</body>
</html>

Open in new window

tks
Here - you were using the wrong event. it is keyup, not onkeyup

http://jsfiddle.net/mplungjan/Uvsrp/
$(document).ready(function() {
  var inputs = $('input[id^="txtquantity"]');
    inputs.live('keyup', function() {
    var total=0,val;
    inputs.each(function() {
      val=$(this).val();
      total+=isNaN(val)?0:parseInt(val,10);
    });
    $("#result").text(total);
  });
});

Open in new window

Hmm, have to be VERY fast to keep up with the accepts here :O

Good night