Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling JS funtion with paramater & If/Else 2

Status
Not open for further replies.

Dummy99

Programmer
Apr 25, 2002
30
FR
Nothing happens when I click the Checkboxes ON or OFF. When ON I should get a calculated price based on Qty & when OFF, I should get a zero price.
Can you tell me what’s wrong? I’ve checked over & over and I believe my code is according to JS documentation.

Various versions of my code indicate:
1 - When using parameter passing, the function doesn’t get called
2 - Without parameter passing & using only one IF and no ELSE, the routine will work although not very well – the checkbox value is always “yes” whether I click it on or off. As soon as I use an ELSE or ELSE IF, I get “Error on page”.

Code hereunder.
Thank you,
Michael

<html>
<head>
<title>Task Selection</title>
</head>
<a name="#Start"></a>

<script type="text/javascript">
function mycalc(mysel)
{
alert (mysel)
if mysel = "one"
{
if (document.getElementById('Benefits').value = "yes")
{document.getElementById("pr01").value = (225 * (document.getElementById('q01').value))}
else
{document.getElementById("pr01").value = 0}
}
else if mysel = "two"
{
if (document.getElementById('Visit').value = "yes")
{document.getElementById("pr02").value = (150 * (document.getElementById('q02').value))}
else
{document.getElementById("pr02").value = 0}
}

document.getElementById("total").value = (document.getElementById("pr01").value * 1) + (document.getElementById("pr02").value * 1)
}
</script>

<noscript>Your browser does not support JavaScript!</noscript>

<center><b>
<font color ="Blue" size=6>
TASKS SELECTION
</b></font>

<font color ="Blue" size=2><p align=left>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;
<font color ="Blue" size=2>Price
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
<font color ="Blue" size=2>Qty
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
<font color ="Blue" size=2>|Task Names (Click/Check the box of desired tasks.)
</font>

<font color="Black">
<br>
&#160;&#160;&#160;&#160;
<input type="Number" size=7 id="pr01" value=0>
<input type="Number" size=2 id="q01" value=1>
<input name="Benefits" type="checkbox" onclick="mycalc("one")">
Accident Benefits Claim Form Package

<br>
&#160;&#160;&#160;&#160;
<input type="Number" size=7 id="pr02" value=0>
<input type="Number" size=2 id="q02" value=1>
<input name="Visit" type="checkbox" onclick="mycalc("two")">
SABS-Visit with authorization

<br>
&#160;&#160;&#160;&#160;
<input type="Number" size=7 id="total" value=0>
&#160;
=&#160;Total Cost
</p></font>

</form>
</body>
</html>

 
Don't check the state of a checkbox with the value, it'll always have the same value, rather check the status of the checkbox with
Code:
document.getElementById(idOfCheckbox).checked
this code returns a true or false value
 
Also, FYI, the checkbox onclick events don't fire the mycalc function because your parameter is within double quotes.

Instead of:
Code:
<input name="Benefits" type="checkbox" onclick="mycalc("one")">

It should be:
Code:
<input name="Benefits" type="checkbox" onclick="mycalc('one')">

Or something to that extent.

Kate

[small]"Forever dork"[/small]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top