473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to change data in multiple textfields dynamically

hi..let's say I have 3 textfields, texfield A must be filled first, then textfield B and textfield C can be filled either one. If textfield B is filled, then for example (textfield A * textfield B), the answer will be shown at textfield C or vice versa.

Below is part of my code.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function calculate()
  3. {
  4.     if(form.textfieldC.value != "")
  5.     {
  6.         var p_Deposit = document.getElementById("textfieldB").value = ((document.form.textfieldC.value / document.form.textfieldA.value) * 100);
  7.     }
  8.  
  9.     else if(form.textfieldB.value != "")
  10.     {
  11.         var a_Deposit = document.getElementById("textfieldC").value = (document.form.textfieldA.value * (document.form.textfieldB.value/100));    
  12.     }
  13. }
  14. </script>
  15.  
Expand|Select|Wrap|Line Numbers
  1. <td><input type="text" name="textfeildB" id="textfeildB"  onKeyUp="calculate()"><span class="font"> (%)</span></td>
  2. <td><input type="text" name="textfeildC" id="textfeildC" onKeyUp="calculate()"></td>
  3.  
This doesn't work because since the textfield A is filled out first, then the rest two textfields is empty, the javascript will detect they are in empty state, so it directly read the else statement. Is anybody know the solution to solve this problem? Thanks in advance.
Sep 28 '10 #1
9 1972
Dormilich
8,658 Expert Mod 8TB
I wouldn’t allow text field C to be editable, but only used for result display. further, I’d use the blur event since this guarantees the user finished typing.
Sep 28 '10 #2
RamananKalirajan
608 512MB
You can do it pa. That's not a big deal. You can do it.

Sample HTML:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="A" name="A" />
  2. <input type="text" id="B" name="B" onKeyUp="calculate(this.value,document.getElementById('C'))"/>
  3. <input type="text" id="C" name="C" onKeyUp="calculate(this.value,document.getElementById('B'))"/>
Sample Script

Expand|Select|Wrap|Line Numbers
  1. function calculate(val, tgtEl){
  2.   var aVal = document.getElementById('A').value;
  3.   if(aVal!="")
  4.   {
  5.      tgtEl.value = val*aVal;
  6.   }
  7.   else{
  8.      alert("Please enter the value for A");
  9.   }
  10. }
I guess by this way you can solve your problem.

Note: The above code is what I have typed. I am not sure whether it will run when you copy the code.

Thanks and Regards
Ramanan Kalirajan
Sep 28 '10 #3
Hi, Dormilich, thanks for the reply. Ya, I know I should allow one of the textfield to be editable only, but in this case, the user is allowed to decide whether textfield B is going to fill or textfield C. So is there any other methods to solve this problem?
Sep 28 '10 #4
Hi, Ramanan Kalirajan, thanks for the reply. I have two different equations and these two different equations is based on user input in which textfield. So I still have the problem about this. Besides, I am not really clear about the variables that you used (val, tgtEl).
Sep 28 '10 #5
RamananKalirajan
608 512MB
The variable val and tgtEl are value of the current element and the target element object. If you see the HTML code you would find the parameter passed to the function.

For you problem, then have two different functions in the input. So if the user is entering text in textbox B means value will be displayed in textbox C and vice versa.

Thanks and Regards
Ramanan Kalirajan
Sep 28 '10 #6
Hi, Ramanan Kalirajan, thanks for the reply. I had solved my problem already without using any variables. Is there any weaknesses without using any variables? Because I find that most of the programmers will use variables to solve this kind of problem. Thanks again for your solution.
Sep 29 '10 #7
Hi, I have another question which related to the question above. For now, I have a drop down menu and new textfield D.
Expand|Select|Wrap|Line Numbers
  1. <select name="type" id="type">
  2. <option value="">-- Please select --</option>
  3. <option value="typeA" onChange="selectA()">Type A</option>
  4. <option value="typeB" onChange="selectB()">Type B</option>
  5. </select>
  6. <input type="text" name="textfieldD" id="textfieldD">
  7.  
The type selected by the user will affect on different functions. For example, if type A was chosen, then the result will display on textfield D will be based on the function selectA().
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function selectA()
  3. {
  4.    calculate();
  5.    document.getElementById("textfieldD").value = a_Deposit; //a_Deposit is a variable that I used before this for the value of textfieldC
  6. }
  7. </script>
  8.  
But if user select the type B, then the result which will display on textfield D will be based on the function selectB().
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function selectB()
  3. {
  4.    calculate();
  5.    document.getElementById("textfieldD").value = a_Deposit * 10;
  6. }
  7. </script>
  8.  
My problem is the value of textfieldA, textfieldB and textfieldC are not affected by the selection of the user from drop down menu. But the value of textfieldD will affect by drop down menu and textfieldC. That means I need to make sure that the change on the selection of drop down menu by the user should be detect in the beginning, start from the value of textfieldA (since textfieldA will affect the value of textfieldB and textfieldC, after that textfieldC will affect textfieldD). But I am not sure what I can do to solve this problem? Besides, I just try to create function selectA() and selectB() but it doesn't work. Do anyone know how to solve this problem? Thanks in advance.
Sep 29 '10 #8
RamananKalirajan
608 512MB
The problem is onchange event should be given to Select not to the option. Fix that one. It will solve.

Thanks and Regards
Ramanan Kalirajan
Sep 29 '10 #9
Thanks. Now I implement my coding as below.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function calAmount()
  3. {
  4.     if(document.getElementById("textfieldA").value != "")
  5.     {
  6.         var a = document.getElementById("textfieldC").value = (document.getElementById("textfieldA").value * (document.getElementById("textfieldB").value/100));    
  7.     }
  8.  
  9.     else
  10.     {
  11.         alert("Please insert a value.");
  12.         document.getElementById("textfieldB").value = "";
  13.         main.textfieldA.focus();
  14.     }
  15. }
  16.  
  17. function calPercent()
  18. {
  19.     if(document.getElementById("textfieldA").value != "")
  20.     {
  21.         var p = document.getElementById("textfieldB").value = ((document.getElementById("textfieldC").value / document.getElementById("textfieldA").value) * 100);
  22.     }
  23.  
  24.     else
  25.     {
  26.         alert("Please insert a value.");
  27.         document.getElementById("textfieldC").value = "";
  28.         main.textfieldA.focus();
  29.     }
  30. }
  31.  
  32. function selectType()
  33. {
  34.     if(document.main.type.options[document.form.type.selectedIndex].value == "TypeA")
  35.     {
  36.         if(document.getElementById("textfieldB").value != "")
  37.         {
  38.             calAmount();
  39.         }
  40.  
  41.          else if(document.getElementById("textfieldC").value != "")
  42.         {
  43.             calPercent();
  44.         }        
  45.    }
  46.  
  47.    else if(document.main.type.options[document.form.type.selectedIndex].value == "TypeB")
  48.    {}    
  49. }
  50. </script>
  51.  
Expand|Select|Wrap|Line Numbers
  1. <select name="type" id="type" onChange="selectType()">
  2. <option value="">-- Please select --</option>
  3. <option value="typeA">Type A</option>
  4. <option value="typeB">Type B</option>
  5. </select>
  6.  
However, I am facing the problem when insert the value into each textfield. Is there any problem with my coding? Thanks in advance.
Sep 30 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: C. Armour | last post by:
Help me, I'm suffering! Situation: I have a load() function which loads a bunch of TextFields with values. There are TextListeners registered for each of these TextFields. Thus, changing the...
1
by: Veverita | last post by:
Hi there I'm hoping that someone can help me with a question I have about javascript syntax. I got an html page that uploads an image and some text field to a database. What I'd like to do...
1
by: Suresh | last post by:
How do I insert a char into the textfield on the keypress event of another textfield? I have two textboxes, "text1" and "text2". Important: "text1" is outside the table. "text2" is inside...
3
by: Sidney Linkers | last post by:
Hi, I'm trying to make a calculated text field in a query where the textvalue is being populated from multiple records. I already use a VBA function to loop through records and concatenate the...
3
by: Cah Sableng | last post by:
Hi all. I am new member of this group. I have a problem with currency formatter function I made. On onkeyup's method of a textfield I add function below: <snip> function formatCurrency(elm)...
9
by: learning | last post by:
Hi! Here's my situation: I have one textfield with one 'submit' button in PAGE1.PHP. When I click on the 'submit' button I am sent to PAGE2.PHP where I have a "switch" routine that checks...
2
jkmyoung
by: jkmyoung | last post by:
I was trying to create an applet with a TextField that would only accept an integer, and ignore any other keystrokes. Eg, if a user typed in an 'f' into the field, the TextField should ignore it, and...
0
by: adamselearning | last post by:
Hi... I've followed Colin Moock's AS3 notes to create a VirtualPet I've got it working from the code at: http://www.adobe.com/devnet/actionsc...n_moock_f6.pdf ...but am now trying to add a...
8
by: ALTAFAD | last post by:
How to copy selected (s) textfield, which those selected through checkbox I like to copy in clipboard. Access textfield data when front of the textfield checkbox is checked if not checked it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.