| | |
passing html input to js file???
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
I am trying to write a simple program to calculate income taxes owed based on gross wages that are input by a user. The HTML page needs to accept the input (wages from 4 users) at once and then utilize a button to submit the information to an external javascript file which retrieves the entered data with one function and then uses another function to calculate the income taxes owed. I have most of my HTML file and my JavaScript file completed, however, I am having a very difficult time with my submit button sending the 4 user's inputs to my external JS file and then getting the results back to my HTML file.
Can someone please help me to understand how to send this information to my external js file and then have my js file send the results back. I have tried many different things thus far, but nothing has gotten me any further and I have zero communication between my js file and my html file. I think having the four different inputs that need to pass back to my js file may be causing me to over think things. Therefore, I am just looking for a little help in understanding how this would work. Any help or advice anyone can give me would be greatly appreciated.
Ps: I am not supposed to place any javascript into my html file.
Thanks in advance and I look forward to hearing from one of the experts on this very helpful site.
Can someone please help me to understand how to send this information to my external js file and then have my js file send the results back. I have tried many different things thus far, but nothing has gotten me any further and I have zero communication between my js file and my html file. I think having the four different inputs that need to pass back to my js file may be causing me to over think things. Therefore, I am just looking for a little help in understanding how this would work. Any help or advice anyone can give me would be greatly appreciated.
Ps: I am not supposed to place any javascript into my html file.
Thanks in advance and I look forward to hearing from one of the experts on this very helpful site.
The script in case of a SCRIPT element may be defined inside the contents of the SCRIPT element or in an external file. The only difference here is that an additional request is made in the case of an external script file. Hence you can treat your script as though it was embedded in the document even when it comes from an external source.
You have a few options; either pass in the `id' / `name' of the form elements whose values you need to access to the Javascript function or just access them directly from your function assuming that the form elements with the given `name' / `id' are always present.
You have a few options; either pass in the `id' / `name' of the form elements whose values you need to access to the Javascript function or just access them directly from your function assuming that the form elements with the given `name' / `id' are always present.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<input type="text" name="txtOne" id="txtOne"> <input type="text" name="txtTwo" id="txtTwo"> <input type="button" value="Calculate" onclick="getIncomeTax('txtOne', 'txtTwo');"> <!-- OR --> <input type="text" name="txtOne" id="txtOne"> <input type="text" name="txtTwo" id="txtTwo"> <input type="button" value="Calculate" onclick="getIncomeTax(this.form);"> function getIncomeTax(frm) { var e1 = frm.elements['txtOne']; var e2 = frm.elements['txtTwo']; var iTax = doSomething(e1.value, e2.value); }
I don't accept change; I don't deserve to live.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
SOS, thanks for your help. That was exactly the information that I was looking for. I was very close to both of those options but I must have typed something in incorrectly because they were not working for me before. However, it is now working. Thanks again and I appreciate your time and assistance. Have a great day.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
One more quick question for you or anyone: Now that I have the information passing from the HTML to the JS file function, how can I get this information to transfer to my second function within the same js file which calculates the taxes owed. I have tried using the same parameters that I used with my function that retrieved the data from my form, but the information is not passing through to my second function. I have set up alerts within both functions on multiple occasions and the first function which is supposed to retrieve the information from the HTML works as it should, but the second function is not receiving the income information to perform the appropriate calculations. Thanks again for your time and assistance in advance.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
Thanks again SOS and hopefully this should be my last question. Is it acceptable coding practice to place my tax calculation coding within my first function (since all that is doing is retrieving user input from my html page, instead of creating a second function? I have done this and everything appears to work very well.
Last edited by jobojo; Nov 4th, 2008 at 3:38 pm.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
SOS thank you for all of your help. I was able to figure out that the way I did my code was not the best nor the "proper" way to do it. However, I was able to figure out how to use the second function so that I could reuse it for all 4 inputs. Trial and error (along with some great advice) on this one has really helped me to learn a lot. Thanks again and take care.
> Is it acceptable coding practice to place my tax calculation coding within my first function
It would be better if you have two functions: one which is invoked onclick of a button and which reads the values from various form fields and the second which is purely involved with the calculation of tax. This way you separate out the data retrieval/validation part from the actual logic, in your case, tax calculation.
It would be better if you have two functions: one which is invoked onclick of a button and which reads the values from various form fields and the second which is purely involved with the calculation of tax. This way you separate out the data retrieval/validation part from the actual logic, in your case, tax calculation.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Basic Perl Web Upload Script (Perl)
- Problem when passing arrays from c# to matlab (C#)
- Passing variables from login page problem. (ASP)
- Passing a Trimmed Value from a FormField (ASP)
- Passing Information help (ASP)
- Script only displaying last entries made (Perl)
- Passing Form Values Problem (ASP)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Zend PHP Certification (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Usefull Prototype
- Next Thread: Soon new year!
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser bug captchaformproblem cart checkbox child class close codes cookies createrange() css cursor date debugger decimal dependent design disablefirebug dom dropdown editor element embed engine enter events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post programming progressbar prototype redirect runtime safari scale scriptlets scroll search security session shopping size software toggle unicode variables web wysiwyg \n






