Solved

calculation between 2 text box

Posted on 2009-05-03
6
307 Views
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.
0
Comment
Question by:gamebits
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24291536
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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24291538
The initial "?>" was not supposed to be there... :)
0
 
LVL 10

Accepted Solution

by:
bugada earned 250 total points
ID: 24291560
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 28

Author Comment

by:gamebits
ID: 24291595
@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
 
LVL 10

Expert Comment

by:bugada
ID: 24291798
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
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 250 total points
ID: 24291901
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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question