calculation between 2 text box

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.
LVL 28
gamebitsAsked:
Who is Participating?
 
bugadaConnect With a Mentor Commented:
try this
<html>
  <head>
  
  <script type="text/javascript">
      
      window.onload = function(e) {
      
         var text1 = document.getElementById("text1");
         var text2 = document.getElementById("text2");
         
         var sum = parseInt(text1.value) + parseInt(text2.value); 
         
         text1.onchange = function(e) {
            text2.value = sum - this.value;
         }
         
         text2.onchange = function(e) {
            text1.value = sum - this.value;
         }
      }
      
      
      
      
  </script>
 
  </head>
  <body>
  
   <input type="textbox" id="text1" value="20">
   <input tyoe="textbox" id="text2" value="0">
   
 
  </body>
</html>

Open in new window

0
 
Roger BaklundCommented:
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;
    if(b.value<0)
      b.value = 0;
  }
  if(X==b) {
    a.value = 20 - b.value;
    if(a.value<0)
      a.value = 0;
  }
}
</script>
<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

0
 
Roger BaklundCommented:
The initial "?>" was not supposed to be there... :)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
gamebitsAuthor Commented:
@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.
0
 
bugadaCommented:
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.
<html>
  <head>
  
  <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;
            }            
         }
         
      }
      
      
      
      
  </script>
 
  </head>
  <body>
  
   <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>
   <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>
   </div>
   
 
  </body>
</html>

Open in new window

0
 
Roger BaklundConnect With a Mentor Commented:
To make my code work in IE, add "var" at the start of lines 3 and 4:

  var a = document.getElementById('a');
  var b = document.getElementById('b');

To allow for any number of pairs, I have modified it slightly:
<script type="text/javascript">
function chg(current,other) {
  var a = document.getElementById(current);
  var b = document.getElementById(other);
  b.value = 20 - a.value;
  if(b.value<0)
    b.value = 0;
}
</script>
<input id="a" type="text" size="2" value="20" onchange="chg('a','b')" />
<input id="b" type="text" size="2" value="0" onchange="chg('b','a')" />
<br />
<input id="c" type="text" size="2" value="20" onchange="chg('c','d')" />
<input id="d" type="text" size="2" value="0" onchange="chg('d','c')" />

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.