We help IT Professionals succeed at work.

calculation between 2 text box

gamebits asked
Last Modified: 2012-05-06
very simple question here for a Guru

given 2 text box how can I change the value of 1 text box by entering a value in another text box and vice versasome example
start with
[20]  [0] change the 0 for a 2 and you get [18]  [2] //20 and 0 are values from db
change [18] for [19] and you get [19]  [1]
change [19] for [21] and you get [21]  [0]  here by going over 20 I do not want to get a negative number in the other box.

What if I have multiple boxes dynamically generated (multiple pair).

This is in relation with another question I have open where I may be asking to much so if I can have the basic I may be able to work something out.
Watch Question

Top Expert 2008

Not sure if I understand, but try this:
?><script type="text/javascript">
function chg(X) {
  a = document.getElementById('a');
  b = document.getElementById('b');
  if(X==a) {
    b.value = 20 - a.value;
      b.value = 0;
  if(X==b) {
    a.value = 20 - b.value;
      a.value = 0;
<input id="a" type="text" size="2" value="20" onchange="chg(this)" />
<input id="b" type="text" size="2" value="0" onchange="chg(this)" />

Open in new window

Top Expert 2008

The initial "?>" was not supposed to be there... :)
This one is on us!
(Get your first solution completely free - no credit card required)


@cxr code is not working in IE but working fine in FF

@bugada code is working fine in both browser but allows for negative number (I'd like not) because the script will be use to manage baseball card collection.

Now, what if I have multiple pairs of boxes I can make them with different id by adding a counter to the id name but how to handle that in the javascript function, I do not know ahead of time how many pair there will be.

Depending on your layout this can be done by adding two divs,one with left inputs, an one with right ones.
You can add how many inputs you like, paying attention to mantain the same number in each div.

The input id must follow this rule: YOURIDa for left input, YOURIDb for right input where YOURID is supposed to be numerical. You can change this direction by altering the events callback accordingly.
  <script type="text/javascript">
      window.onload = function(e) {
         var lInputs = document.getElementById("left").getElementsByTagName("input");
         var rInputs = document.getElementById("right").getElementsByTagName("input");
         var sum = 20;
         for (var i = 0; i < lInputs.length; i++) {
            lInputs[i].onchange = function(e) {
               var val = this.value > sum ? 0 : sum - this.value;
               document.getElementById(this.id.replace("a","b")).value = val
            rInputs[i].onchange = function(e) {
               var val = this.value > sum ? 0 : sum - this.value;
               document.getElementById(this.id.replace("b","a")).value = val;
   <div id="left" style="float:left">
      <input type="textbox" id="1a" value="20"><br>
      <input type="textbox" id="2a" value="20"><br>
      <input type="textbox" id="3a" value="20"><br>
      <input type="textbox" id="4a" value="20"><br>
      <input type="textbox" id="5a" value="20"><br>
   <div id="right" style="float:left">
      <input type="textbox" id="1b" value="20"><br>
      <input type="textbox" id="2b" value="20"><br>
      <input type="textbox" id="3b" value="20"><br>
      <input type="textbox" id="4b" value="20"><br>
      <input type="textbox" id="5b" value="20"><br>

Open in new window

Top Expert 2008
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.