Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    pml
    pml is offline
    New Coder
    Join Date
    Mar 2005
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Why can't I use "document.write" here+2 other questions

    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>

  • #2
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Check this out....

    Code:
    <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.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Hi afru

    I answered this on wd

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

  • #4
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •