Forum Moderators: open
<script language="JavaScript" type="text/javascript">function calcrows(){
var carbon1 = (document.carbontool.weddingsqft.value);
var carbon2 = (document.carbontool.receptionsqft.value);
var carbon3 = (document.carbontool.othersqft.value);
var weddingsqftkwh = ((carbon1*15.1)/365));
var receptionsqftkwh = ((carbon2*15.1)/365));
var othersqftkwh = ((carbon3*15.1)/365));
var electricityco2e = ((weddingsqftkwh+receptionsqftkwh+othersqftkwh)*0.0007246);
document.carbontool.weddingsqftkwh.value = weddingsqftkwh;
document.carbontool.receptionsqftkwh.value = receptionsqftkwh;
document.carbontool.othersqftkwh.value = othersqftkwh;
document.carbontool.electricityco2e.value = electricityco2e;
}
function roundVal(val){
var dec = 2;
var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
</script>
....and then in the body
<td align="right" class="line3" colspan="2"><b>Answer: <script language="JavaScript" type="text/javascript">document.write(roundVal(electricityco2e));</script></b></td>
2. Don't use scripts inline in your HTML.
3. Don't use document.write. It is sloppy and will overwrite your document if called after the page has finished loading. Instead, us DOM methods or innerHTML to append content to your DOM.
How would I make the following code happen when onchange happens?
var carbon1 = (document.carbontool.weddingsqft.value);
var carbon2 = (document.carbontool.receptionsqft.value);
var carbon3 = (document.carbontool.othersqft.value);
I assume if any of these change you wish to recalculate result ?
BTW brackets not required
so on each of these say
<input type="text" name="weddingsqft" onchange="calcrows()" ...
change
<script language="JavaScript" type="text/javascript">document.write(roundVal(electricityco2e));</script>
to
<span id="answer">...</span>
at end of function calcrows add
document.getElementById("answer").innerHTML = roundVal(electricityco2e);