adding or subtracting value when typing in textbox

Seven price
Seven price used Ask the Experts™
on
 //         $(window).load(function() {
                   $(document).ready(function() {
                      //add functions here
                       $('input[id^="txtquantity"]').live('keyup', function() {
                       //  if (String($(this).val()).length < 1) return false;
                         getSum();
                       });
                 });
          $(document).ready(function() {
              getSum();
          });
function getSum() {

              var s = 0;
              $('input[id^="txtquantity"]').each(function() {
                  //if (String($(this).val()).length < 1) return false;
                  var v = $(this).val();

                  s = s + (isNaN(v) ? 0 : parseInt(v));

              });
              $('#lblSum').html(s);
          }    


Ok Basically I
will pass a value to
Var s
example var s = '50'

Is there a way to pass a value but it only register when keying in a number into my textbox.
So example if I have values in the textbox it will not read these values only the values I type in.
<input type="text" id="txtquantity1" value="10" />
<input type="text" id="txtquantity2" value="20" />
<input type="text" id="txtquantity3" value="5" />
<input type="text" id="txtquantity4" value="5" />
<input type="text" id="txtquantity5" value="10" />

So if I have a sum of 50 in var s and I type in 5 in id=txtquantity5 it will change the value to 45 but I think the only way this will work if it never loads the values from the textboxes but only the value I type in. Reason is because I am passing the combined value to var  S. So it will not have to add the value just subtract or add the value when I type it in.
Understand I hope.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

Could you rephrase your question please, not quite sure what your asking, this is what I have:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">           
            $(function() {
                $('input[id^="txtquantity"]').live('keyup', function() {
                    getSum();
                });
            });
            function getSum() {
                var s = 0;
                $('input[id^="txtquantity"]').each(function() {
                    var v = $(this).val();
                    s = s + (isNaN(v) ? 0 : parseInt(v));                
                });
                $('#lblSum').html('<p>'+s+'</p>');
            }
        </script>
    </head>
    <body>
        <button id="show">Show Length of Queue</button>
        <span></span>
        <div></div>
        <input type="text" id="txtquantity1" value="10" />
        <input type="text" id="txtquantity2" value="20" />
        <input type="text" id="txtquantity3" value="5" />
        <input type="text" id="txtquantity4" value="5" />
        <input type="text" id="txtquantity5" value="10" />
        <div id ="lblSum"></div>
    </body>
</html>

Open in new window


Do you want to start with a default value and subtract it from 50?

Darren
Seven priceFull Stack

Author

Commented:
tks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial