...

View Full Version : Why can't I use "document.write" here+2 other questions



pml
03-13-2005, 04:43 PM
I wonder why I can't use the "document.write (Total)" in the end. The sum should also change evertime
a form element is clicked and be displayed on the page as text(not by using an alert box like function "Calculate2").
In which ways would the answer change if I had prefered the sum to be displayed in another frame, named "frame2"?
The form below is in "frame1".

Unfortunately I can't get the checkboxes to behave like the radio buttons either.

Can anyone help me with these problems?

<html>
<head>

<script language="javascript" type="text/javascript">
var MyArray = new Array(0,0,0,0);

function Calculate (nr, value) {
MyArray[nr] = value;
Calculate2();
}

function Calculate2() {

var Total = parseInt(document.MyForm.SecretField.value);
var JoinedArray = MyArray.join("+");
Total = Total + eval(JoinedArray);
//alert ("Total value is" + Total);
}
</script>
</head>

<body onLoad="Calculate2()">


<p>
<form name="MyForm">
<p>The default value is 10.</p>
<p>
<input name="RadioGroup1" type="radio" value="SomeValue1" checked onClick="Calculate(0, 0)">
<br>
<input name="RadioGroup1" type="radio" value="SomeValue2" onClick="Calculate(0, 2)">
Add 2 <br>
<input name="RadioGroup1" type="radio" value="SomeValue3" onClick="Calculate(0, 3)">
Add 3 <br>
<input name="check1" type="checkbox" id="check1" value="SomeValue4" onClick="Calculate(1, 8)">
Add 8</p> </p>
<p>
<input name="RadioGroup2" type="radio" value="SomeValue5" checked onClick="Calculate(2, 0)"> <br>
<input name="RadioGroup2" type="radio" value="SomeValue6" onClick="Calculate(2, 3)">
Add 3 <br>
<input name="RadioGroup2" type="radio" value="SomeValue7" onClick="Calculate(2, 9)">
Add 9 <br>
<input name="check2" type="checkbox" value="SomeValue8" onClick="Calculate(3, 12)">
Add 12 </p>
<input type="hidden" name="SecretField" value="10">
</form>
The total value is:
<script language="javascript" type="text/javascript">
doucment.write (Total);
</script>

</body>
</html>

afru
03-13-2005, 05:21 PM
Check this out....



<html>
<head>

<script language="javascript" type="text/javascript">
var MyArray = new Array(0,0,0,0);

function Calculate (nr, value) {
MyArray[nr] = value;
Calculate2();
}

function Calculate2() {

var Total = parseInt(document.MyForm.SecretField.value);
var JoinedArray = MyArray.join("+");
Total = Total + eval(JoinedArray);
document.getElementById("result").innerHTML="Result : "+Total;
//alert ("Total value is" + Total);
}
</script>
</head>

<body onLoad="Calculate2()">


<p>
<form name="MyForm">
<p>The default value is 10.</p>
<p>

<input name="RadioGroup1" type="radio" value="SomeValue1" checked onClick="Calculate(0, 0)">
<br>
<input name="RadioGroup1" type="radio" value="SomeValue2" onClick="Calculate(0, 2)">
Add 2 <br>
<input name="RadioGroup1" type="radio" value="SomeValue3" onClick="Calculate(0, 3)">
Add 3 <br>
<input name="check1" type="checkbox" id="check1" value="SomeValue4" onClick="Calculate(1, 8)">
Add 8</p> </p>
<p>
<input name="RadioGroup2" type="radio" value="SomeValue5" checked onClick="Calculate(2, 0)"> <br>
<input name="RadioGroup2" type="radio" value="SomeValue6" onClick="Calculate(2, 3)">
Add 3 <br>
<input name="RadioGroup2" type="radio" value="SomeValue7" onClick="Calculate(2, 9)">
Add 9 <br>
<input name="check2" type="checkbox" value="SomeValue8" onClick="Calculate(3, 12)">
Add 12 </p>
<input type="hidden" name="SecretField" value="10">
</form>


<div id="result" ></div>

</body>
</html>


You cannot use document.write to change contents dynamically. If you want to do it use innerHTML instead. By the way, I have not changed any calculations, you can change them as per your need....

Have fun :)

Afru.

vwphillips
03-13-2005, 05:51 PM
Hi afru

I answered this on wd

I came up with much same as you for doc..write but he did ask for checkboxes

afru
03-13-2005, 06:06 PM
Hi philips,

Nice to meet you, I think the code is working with checkboxes too... I have tested it before posting. Anyhow, your idea about changing textbox background onfoucs (on some other post) is cool. I will definetly use it in my next project.

Have a nice day :)

Afru.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum