I was making a calculator for a game and made a button like this:
<input type="button" value="Calculate" name="Calc_Button" onclick="cpbCalc()" class="button_is">
When button is clicked, I want to run the cpbCalc()
function.
You can find this html code live on http://trial.6te.net/Calculators/cpbCalculatorNew.html
Here is the complete html Code :
<!DOCTYPE html>
<html>
<head>
<title>
Cost Per Battle Calculator
</title>
</head>
<style>
.smaller{
width: 50px;
padding: 12px 10px;
margin: 0px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.bigger{
width: 110px;
padding: 12px 10px;
margin: 0px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button_is{
width: 110px;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 0px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button_is:hover {
background-color: #45a049;
}
div {
border-radius: 5px;
background-color: #badf6f;
padding:20px;
}
</style>
<body>
<div>
<!-- <label for="fname" id="fn">First Name</label> -->
<!-- <input type="text" id="fname" name="firstname"> -->
<!-- <button onclick="myFunction()">Calculate</button> -->
<table border="0">
<tr>
<td>
<center><img src="http://trial.6te.net/images/gold.gif"></img></center>
</td>
<td>
<center><img src="http://trial.6te.net/images/wood_s.gif"></img></center>
</td>
<td>
<center><img src="http://trial.6te.net/images/ore_s.gif"></img></center>
</td>
<td>
<center><img src="http://trial.6te.net/images/mercury_s.gif"></img></center>
</td>
<td>
<center><img src="http://trial.6te.net/images/sulphur_S.gif"></img></center>
</td>
<td>
<center><img src="http://trial.6te.net/images/crystal_S.gif"></img></center>
</td>
<td>
<center><img src="http://trial.6te.net/images/gems_S.gif"></img></center>
</td>
</tr>
<tr>
<td>
<input type="text" id="gold_" name="Gold" class="bigger">
</td>
<td>
<input type="text" id="wood_" name="Wood" class="smaller" value="0">
</td>
<td>
<input type="text" id="ore_" name="Ore" class="smaller" value="0">
</td>
<td>
<input type="text" id="mercury_" name="Mercury" class="smaller" value="0">
</td>
<td>
<input type="text" id="sulphur_" name="Sulphur" class="smaller" value="0">
</td>
<td>
<input type="text" id="crystals_" name="Crystals" class="smaller" value="0">
</td>
<td>
<input type="text" id="gems_" name="Gems" class="smaller" value="0">
</td>
</tr>
<tr>
<td>
Durability :<br>
<input type="text" id="currDura_" name="Current_Durability" class="smaller"> /
</td>
<td>
<input type="text" id="maxDura_" name="Maximum_Durability" class="smaller">
</td>
</tr>
<tr>
<td>
Repair Cost :<br>
<input type="text" id="repCost_" name="Repair_Cost" class="bigger">
</td>
</tr>
<tr>
<td>
Smith Efficiency :<br>
<input type="text" id="smithEffi_" name="Smith_Efficiency" class="smaller">
</td>
<td>
Smith Charges :<br>
<input type="text" id="smithCharge_" name="Smith_Charge" class="smaller">
</td>
</tr>
<tr>
<td colspan="7">
<center><input type="button" value="Calculate" name="Calc_Button" onclick="cpbCalc()" class="button_is"></center>
</td>
</tr>
<tr>
<td colspan="7">
<label id="result_"></label>
</td>
</tr>
</table>
</div>
<script language="JavaScript">
<!--
function cpbCalc() {
var currDura, maxDura, tempMaxDura, tempDura, totDura, optDura;
var iniCost, repCost;
var smithEffi, smithCharge;
var se, sc;
var totCostTillNow, costPerBattle = 0, minCPB;
var i;
var repCount = 1;
//Assigning the values
currDura = document.getElementById("currDura_").value;
maxDura = document.getElementById("maxDura_").value;
iniCost = document.getElementById("gold_").value;
repCost = document.getElementById("repCost_").value;
smithEffi = document.getElementById("smithEffi_").value;
smithCharge = document.getElementById("smithCharge_").value;
se = smithEffi / 100;
sc = smithCharge / 100;
tempMaxDura = maxDura;
tempDura = currDura;
totDura = tempDura;
totCostTillNow = parseFloat(iniCost);
costPerBattle = parseFloat(totCostTillNow / totDura);
minCPB = parseFloat(costPerBattle);
optDura = parseInt(tempMaxDura);
for(i=1; i<=maxDura; i++)
{
totCostTillNow += parseFloat(repCost * sc);
tempDura = parseInt(tempMaxDura * se);
totDura += parseInt(tempDura);
costPerBattle = parseFloat(totCostTillNow / totDura);
tempMaxDura -= 1;
if ( minCPB >= costPerBattle )
{
minCPB = parseFloat(costPerBattle);
optDura = parseInt(tempMaxDura);
}
}
document.getElementById("result_").value = eval(minCPB) + " gold at 0/"+ eval(optDura);
return 0;
//alert("minimum cost per battle = " + eval(minCPB) + "at 0/" + eval(optDura));
//-->
}
</script>
</body>
</html>
Here "result_" is a label where I want to provide the final answer.
When I click the button, it should run the function "cpbCalc()
" but it doesn't.
Instead it does nothing.
Also when I check in Console, it shows no errors.
Can you help me why this is occurring and provide a solution for it?