|
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!!!
|
|
|
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...?
|
|
|
|
|
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.
|
|
|
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.
|
|
|
|
|
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!
|
|
|
<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.
|
|
|
|
|