473,382 Members | 1,622 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,382 software developers and data experts.

How to insert a valueof variable into text box?

Well I know how to pass value of an item from drop down list to a text box, but this ones different. I'm very new to javascript and have no idea how to pass value calculated variable into a text box.
Expand|Select|Wrap|Line Numbers
  1. function calct()
  2. {
  3. var un1=document.getElementById("unit").value;
  4. var qn1=document.getElementById("qty").value;
  5. var x1=eval(un1);
  6. var y1=eval(qn1);
  7. var z1=eval(x1*y1);
  8. document.getElementById("subtl").value= z1;
  9. }
  10.  
and then i need the value of the variable subtotal in a text box with id subtl
I think its wrong because it gives me NaN as the value for my text box subtl

And i dont know how to generalize a function. I have multiple variable "unit" (unit1, unit2, unit3... ), "qty" (qty1, qty2, qty3...) and aslo 'subtl" so I was wondering if there is a way to use 1 function for all of them?
Aug 12 '09 #1
5 14516
gits
5,390 Expert Mod 4TB
you shouldn't ever use eval. all values that you retrieve from a node are 'string'-values first ... so you could use the parseFloat() method to cast them to numbers, i.e.:

Expand|Select|Wrap|Line Numbers
  1. var x1 = parseFloat(un1);
in case the value is not parsable as number then x1 will be NaN = NotANumber ... you could check that before the calculation and react with code :)

to the other point: please show a short example for it so that we could follow how you have your fields and how you want to call the function. basicly you could pass the ids of the nodes to the function and then use them as parameters inside the function.

kind regards
Aug 13 '09 #2
There are 6 more rows like this below:
Now I want a way to use the function showprice1 and calc1 in those 6 rows.

Expand|Select|Wrap|Line Numbers
  1. function showprice1()
  2. {
  3. var itm=document.getElementById("item1");
  4. document.getElementById("unit1").value=itm.options[itm.selectedIndex].value;
  5. }
  6. <tr>
  7. <td><select id="item1" onchange="showprice1()">
  8. <option value=0.00 selected>None</option>
  9. <option value=0.95>Shirts</option>
  10. <option value=2.75>Pants</option>
  11. <option value=5.50>Dress</option>
  12. <option value=8.90>Suit</option>
  13. <option value=4.50>Bed Sheets</option>
  14. <option value=3.00>Others</option>
  15. </select>
  16. <script type="text/javascript">
  17. <!--
  18. function calct1()
  19. {
  20. var un1=document.getElementById("unit1").value;
  21. var qn1=document.getElementById("qty1").value;
  22. var x1=parseFloat(un1);
  23. var y1=parseFloat(qn1);
  24. var z1=(x1*y1);
  25. var sb1=z1.toFixed(2);
  26. document.getElementById("sub1").value=sb1;
  27. }
  28. // -->
  29. </script>
  30. <td><input type="text" id="unit1" size="6" maxlength="6" value=0.00 readonly></td>
  31. <td><input type="text" id="qty1" size="6" maxlength="6" value=0 onclick=(this.value="")></td>
  32. <td><input type="submit" value="Calc" onsclick="calct1()"></td>
  33. <td><input type="text" id="sub1" size="6" maxlength="6" value=0.00></td>
  34. </tr>
  35.  
The parseFloat doesn't work. Could you please tell me what I'm doing wrong.
Thank you very much
Aug 13 '09 #3
gits
5,390 Expert Mod 4TB
it works but you have some errors ... have a look at the following working example:

Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2.  
  3. <head>
  4. <script type="text/javascript">
  5.  
  6. function calct1(unId, quId, stId) {
  7.     var un1=document.getElementById(unId).value;
  8.     var qn1=document.getElementById(quId).value;
  9.     var x1=parseFloat(un1);
  10.     var y1=parseFloat(qn1);
  11.     var z1=(x1*y1);
  12.     var sb1=z1.toFixed(2);
  13.     document.getElementById(stId).value=sb1;
  14. }
  15.  
  16. </script>
  17. </head>
  18. <body>
  19.     <input type="text" id="unit1" size="6" maxlength="6" value="8.00" readonly="readonly">
  20.     <input type="text" id="qty1" size="6" maxlength="6" value="0" onclick="(this.value='')">
  21.     <input type="submit" value="Calc" onclick="calct1('unit1', 'qty1', 'sub1')">
  22.     <input type="text" id="sub1" size="6" maxlength="6" value="0.00">
  23.     <br/><br/>
  24.     <input type="text" id="unit2" size="6" maxlength="6" value="8.00" readonly="readonly">
  25.     <input type="text" id="qty2" size="6" maxlength="6" value=0 onclick="(this.value='')">
  26.     <input type="submit" value="Calc" onclick="calct1('unit2', 'qty2', 'sub2')">
  27.     <input type="text" id="sub2" size="6" maxlength="6" value="0.00">
  28. </body>
  29. </html>
  30.  
note that you have a wrong onclick that was onsclick and the onclick to reset the values have to be quoted. i even used the field ids to show you haow to pass that as params to the function to use that function genarally ...

kind regards
Aug 14 '09 #4
Thank you very much.
This was very helpful. I really appreciate your help.

Regards

RD
Aug 15 '09 #5
gits
5,390 Expert Mod 4TB
no problem ... just post back to the forum anytime you have more questions :)

kind regards
Aug 15 '09 #6

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

Similar topics

4
by: MJ | last post by:
Currently i am using the below coding to create a text file and write something on it. Dim wfile As New System.IO.StreamWriter("c:\test.txt" wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf &...
0
by: Thomas | last post by:
Hi! How can i get the optimal richtext width size, when depending on the variable text lengh. The text can be colored or with some differnt fontsizes. I dont want use scrollbars. Is there a...
4
by: keerthana_m_2003 | last post by:
Hello: is it possible to insert the value of some JavaScript variable in the body of HTML (for example the text on button, or at any part of the HTML text part). Please tell me how to do this. ...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
1
by: artev | last post by:
at click, the bottom function insert a box text inner a div-my; but in IE I not have the style; why? others some small questions: is correct the code? this because in some example: is not...
2
by: yeap | last post by:
Hi All, I can't insert java variable into ms access database. I'm using odbc connection to ms access. Below are my coding. try { ...
7
by: shivapadma | last post by:
I want explanation for CLOB datafield 1.I created clobtable with the query create table clobexample(id number,text CLOB); 2.I tried to insert very large text by the following query insert...
2
by: ORB30 | last post by:
Hello all Im having an issue trying to insert a variable in to a database. This variable is the math result of 3 fields of the form, a division multiplied by a number. So far is returning...
1
by: ChiLurker | last post by:
I want to have the browser to be directed to a certain url upon button click with the text inserted into the textbox place into the url, and I'm stumped. for example-...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.