Solved

calculation between 2 text box

Posted on 2009-05-03
6
295 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now