codetoad.com
Home||ASP|ASP.Net|C++/C#|DHTML|HTML|Java|Javascript|Perl|VB|XML||CodeToadPlus!||Forums||RAM
Search Site:
Search Forums:
HELP!! cwilkey at 22:06 on Sunday, November 21, 2004

I need to calculate two form fields and display the results in a third field -- However the calculation is dependant on whether a drop downbox is selected. Here is my attempt ...I should mention that I've never written Javascript before.

<SCRIPT>
<!--
function Calc() {
if (this.form.direct.value = "YES")
this.form.gross_adjustment.value = (this.form.change_from.value - this.form.change_to.value)
this.form.net_adjustment.value = this.form.gross_adjustment.value
} else {
this.form.gross_adjustment.value =(this.form.change_from.value - this.form.change_to.value)
this.form.net_adjustment.value =(this.form.gross_adjustment.value * .85)
}
// -->
</SCRIPT>

Please HELP!!!

Re: HELP!! tgreer at 17:04 on Monday, November 22, 2004

What do you mean, the calculation depends on whether a dropdown value is selected?

Describe the problem in English, such as:

I have a dropdown list, two textboxes, and a button. When the button is clicked, I want...?



Re: HELP!! cwilkey at 18:35 on Monday, November 22, 2004

I have a drop down box and three text fields. If the value of the dropdown box is equal to "YES" then I would like to subtract the value of textbox1 from textbox2 and display the value of the calculation in textbox3. If the value of the dropdown box is equal to "NO" then I would like to subtract the value of textbox1 from textbox2, mulitply it times .85 and display the value of the calculation in textbox3.



Re: HELP!! tgreer at 19:03 on Monday, November 22, 2004

Thanks, here you go:


<html>
<head>
<script type="text/javascript">
function myCalc()
{
var sel = document.getElementById("YesNo");
var t01 = document.getElementById("txt01");
var t02 = document.getElementById("txt02");
var t03 = document.getElementById("txt03");

if (sel.options[sel.selectedIndex].value == "Yes")
{
t03.value = t02.value - t01.value;
}
else
{
t03.value = (t02.value - t01.value) * 0.85;
}
}

</script>
</head>

<body>

<form>
<select id="YesNo"/ onchange="myCalc();">
<option value="Yes"/>Yes
<option value="No"/>No
</select>
<br/>
<input type="text" id="txt01"/><br/>
<input type="text" id="txt02"/><br/>
<input type="text" id="txt03"/><br/>
<input type="button" value="calc" onclick="return myCalc();" />
</form>

</body>
</html>


Please support CodeToad by clicking one of the ads. If you found my post helpful, please visit my site and thank me by clicking one of MY ads.


Re: HELP!! cwilkey at 13:38 on Wednesday, November 24, 2004

This is perfect! I do have an additional favor to ask. I'd like to add a fourth field. What I'm doing is calculating "gross adjustment" and "net adjustment". The gross adjustment should always be textbox1-textbox2. However net adjustment is dependant on the dropdown box. If "YES" is selected then net adjustment should be 100% of gross adjustment (or in other words gross and net adjustment would be the same number) however if "NO" is selected then net adjustment is gross adjustment * .85.

Does that make sense? I can't tell you enough how big of a help you are. Thanks again!

Re: HELP!! tgreer at 14:55 on Wednesday, November 24, 2004



<html>
<head>
<script type="text/javascript">
function myCalc()
{
var sel = document.getElementById("YesNo");
var t01 = document.getElementById("txt01");
var t02 = document.getElementById("txt02");
var t03 = document.getElementById("txt03");
var t04 = document.getElementById("txt04");

t03.value = t02.value - t01.value;

if (sel.options[sel.selectedIndex].value == "Yes")
{
t04.value = t03.value;
}
else
{
t04.value = t03.value * 0.85;
}
}

</script>
</head>

<body>

<form>
<select id="YesNo"/ onchange="myCalc();">
<option value="Yes"/>Yes
<option value="No"/>No
</select>
<br/>
Subtract this: <input type="text" id="txt01"/><br/>
from this: <input type="text" id="txt02"/><br/>
Equals this: <input type="text" id="txt03"/><br/>

Adjusted to:<input type="text" id="txt04"><br/>
<input type="button" value="calc" onclick="return myCalc();" />
</form>

</body>
</html>


I'm glad I could help. Remember to visit my site and click some of the ads, if you'd really like to thank me. Sites like CodeToad support themselves with ad revenue, as well. Click some of their ads. Google ads, in particular, are context-sensitive, so you might just find something useful.









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
matrix addition
Re: Storing data from HTML to Excel or TXT
Re: function within loop problem
Re: Ô‡´ò¥¯¥é¥Ö¤Ï ¥Æ©`¥é©`¥á¥¤¥É£ò£±£±¥¢¥¤¥¢¥ó ¤Î£··¬ ¤Ç¤¹
Re: Replace
Re: タイトリスト AP2アイアン 712�情�
Re: SMS from Perl using HTTP request
Re: Charl Schwartzel
Re: Adhyayan - Annual Student Conference and Online Coding Festival


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


Copyright codetoad.com 2001-2013