Solved

calculation between 2 text box

Posted on 2009-05-03
6
297 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now