Forum Moderators: open

Message Too Old, No Replies

onChange and Javascript

         

weddingm

6:50 am on Nov 20, 2009 (gmt 0)

10+ Year Member



How would I make the following code happen when onchange happens?

<script language="JavaScript" type="text/javascript">document.write(roundVal(weddingsqftkwh));</script>

weddingm

7:16 am on Nov 20, 2009 (gmt 0)

10+ Year Member



where would I put this code to write?

onChange="calcrows()"

daveVk

12:37 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure I understand your question ?

Short answer
onChange="document.write(roundVal(weddingsqftkwh));"

document.write is not a good idea in this case.

Please give more details of what is being monitored for change, and where where you what to "write" the result to.

weddingm

2:13 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



Not sure is I have it all correct as I just started putting it together...


<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:&nbsp;&nbsp;&nbsp;<script language="JavaScript" type="text/javascript">document.write(roundVal(electricityco2e));</script></b></td>

Fotiman

3:49 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Get rid of the language attribute. It's invalid and not needed.

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?

When onchange of what happens?

daveVk

1:12 am on Nov 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I take it from script you have 3 input controls of some kind

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);