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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with Radio Butons

    I have an assignment do tomorrow and the program worked then i saved turned it off and today it don't plz help oh and im not that good so try to be simple :P

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    //Egon Klein
    //W0637104
    //MIT153
    
    function calculate()
    {
    
    //Cm - Cm
    var INPUT = document.getElementById("input");
    if (CM1.checked && CM2.checked) {document.write(INPUT.value)}
    
    
    //Km - Km
    var INPUT = document.getElementById("input");
    if (KM1.checked && KM2.checked) {document.write(INPUT.value)}
    
    
    //Meters - Meters
    var INPUT = document.getElementById("input");
    if (METERS1.checked && METERS2.checked) {document.write(INPUT.value)}
    
    
    //Cm - Meters
    var INPUT = document.getElementById("input");
    if (CM1.checked && METERS2.checked) {document.write(INPUT.value/100)}
    
    
    // Cm - Km
    var INPUT = document.getElementById("input");
    if (CM1.checked && KM2.checked) {document.write(INPUT.value/100000)}
    
    //Meters - Cm
    var INPUT = document.getElementById("input");
    if (METERS1.checked && CM2.checked) {document.write(INPUT.value*100)}
    
    
    //Meters - Km
    var INPUT = document.getElementById("input");
    if (METERS1.checked && KM2.checked) {document.write(INPUT.value/1000)}
    
    
    //Km - Cm
    var INPUT = document.getElementById("input");
    if (KM1.checked && CM2.checked) {document.write(INPUT.value*100000)}
    
    
    //Km - Meters
    var INPUT = document.getElementById("input");
    if (KM1.checked && METERS2.checked) {document.write(INPUT.value*1000)}
    
    
    // Cm - Inches
    var INPUT = document.getElementById("input");
    if (CM1.checked && INCHES2.checked) {document.write(INPUT.value*0.393700787)}
    ///////////////////////////////////////////////////////////////
    
    //Km - Inches
    var INPUT = document.getElementById("input");
    if (KM1.checked && INCHES2.checked) {document.write(INPUT.value*39 370.0787)}
    
    
    //Meters - Inches
    var INPUT = document.getElementById("input");
    if (METERS1.checked && INCHES2.checked) {document.write(INPUT.value*39.3700787)}
    
    
    //Cm - Feet
    var INPUT = document.getElementById("input");
    if (CM1.checked && FEET2.checked) {document.write(INPUT.value*0.032808399)}
    
    
    //Km - Feet
    var INPUT = document.getElementById("input");
    if (KM1.checked && FEET2.checked) {document.write(INPUT.value*3280.8399)}
    
    
    //Meters - Feet
    var INPUT = document.getElementById("input");
    if (METERS1.checked && FEET2.checked) {document.write(INPUT.value*3.2808399)}
    
    
    //Cm - Yard
    var INPUT = document.getElementById("input");
    if (CM1.checked && YARD2.checked) {document.write(INPUT.value*0.010936133)}
    
    
    //Km - Yard
    var INPUT = document.getElementById("input");
    if (KM1.checked && YARD2.checked) {document.write(INPUT.value*1 093.6133)}
    
    
    //Meters - Yard
    var INPUT = document.getElementById("input");
    if (METERS1.checked && YARD2.checked) {document.write(INPUT.value*1.0936133)}
    
    
    //Cm - Miles
    var INPUT = document.getElementById("input");
    if (CM1.checked && MILES2.checked) {document.write(INPUT.value*0.000006213712)}
    
    
    //Km - Miles
    var INPUT = document.getElementById("input");
    if (KM1.checked && MILES2.checked) {document.write(INPUT.value*0.621371192)}
    
    
    //Meters - Miles
    var INPUT = document.getElementById("input");
    if (METERS1.checked && MILES2.checked) {document.write(INPUT.value*0.000621371192)}
    
    
    //Inches - Cm
    var INPUT = document.getElementById("input");
    if (INCHES1.checked && CM2.checked) {document.write(INPUT.value*2.54)}
    
    
    //Inches - Km
    var INPUT = document.getElementById("input");
    if (INCHES1.checked && KM2.checked) {document.write(INPUT.value*0.0000254)}
    
    
    //Inches - Meters
    var INPUT = document.getElementById("input");
    if (INCHES1.checked && METERS2.checked) {document.write(INPUT.value*0.0254)}
    
    
    //Feet - Cm
    var INPUT = document.getElementById("input");
    if (FEET1.checked && CM2.checked) {document.write(INPUT.value*30.48)}
    
    
    //Feet - Km
    var INPUT = document.getElementById("input");
    if (FEET1.checked && KM2.checked) {document.write(INPUT.value*0.0003048)}
    
    
    //Feet - Meters
    var INPUT = document.getElementById("input");
    if (FEET1.checked && METERS2.checked) {document.write(INPUT.value*0.3048)}
    
    
    //Yard - Cm
    var INPUT = document.getElementById("input");
    if (YARD1.checked && CM2.checked) {document.write(INPUT.value*91.44)}
    
    
    //Yard - Km
    var INPUT = document.getElementById("input");
    if (YARD1.checked && KM2.checked) {document.write(INPUT.value*0.0009144)}
    
    
    //Yard - Meters
    var INPUT = document.getElementById("input");
    if (YARD1.checked && METERS2.checked) {document.write(INPUT.value*0.9144)}
    
    
    //Miles - Cm
    var INPUT = document.getElementById("input");
    if (MILES1.checked && CM2.checked) {document.write(INPUT.value*160 934.4)}
    
    
    //Miles - Km
    var INPUT = document.getElementById("input");
    if (MILES1.checked && KM2.checked) {document.write(INPUT.value*1.609344)}
    
    
    //Miles - Meters
    var INPUT = document.getElementById("input");
    if (MILES1.checked && METERS2.checked) {document.write(INPUT.value*1609.344)}
    
    
    //Inches - Inches
    var INPUT = document.getElementById("input");
    if (INCHES1.checked && INCHES2.checked) {document.write(INPUT.value)}
    
    
    //Inches - Feet
    var INPUT = document.getElementById("input");
    if (INCHES1.checked && FEET2.checked) {document.write(INPUT.value*0.0833333333)}
    
    
    //Inches - Yard
    var INPUT = document.getElementById("input");
    if (INCHES1.checked && YARD2.checked) {document.write(INPUT.value*0.0277777778)}
    
    
    //Inches - Miles
    var INPUT = document.getElementById("input");
    if (MILES1.checked && MILES2.checked) {document.write(INPUT.value*0.0000157828283)}
    
    
    //Feet - Feet
    var INPUT = document.getElementById("input");
    if (FEET1.checked && FEET2.checked) {document.write(INPUT.value)}
    
    
    //Feet - Inches
    var INPUT = document.getElementById("input");
    if (FEET1.checked && INCHES2.checked) {document.write(INPUT.value*12)}
    
    
    //Feet - Yard
    var INPUT = document.getElementById("input");
    if (FEET1.checked && YARD2.checked) {document.write(INPUT.value*0.33333333)}
    
    
    //Feet - Miles
    var INPUT = document.getElementById("input");
    if (FEET1.checked && MILES2.checked) {document.write(INPUT.value*0.000189393939)}
    
    
    //Yard - Yard
    var INPUT = document.getElementById("input");
    if (YARD1.checked && YARD2.checked) {document.write(INPUT.value)}
    
    
    //Yard - Feet
    var INPUT = document.getElementById("input");
    if (YARD1.checked && FEET2.checked) {document.write(INPUT.value*3)}
    
    
    //Yard - Inches
    var INPUT = document.getElementById("input");
    if (YARD1.checked && INCHES2.checked) {document.write(INPUT.value*36)}
    
    
    //Yard - Miles
    var INPUT = document.getElementById("input");
    if (YARD1.checked && MILES2.checked) {document.write(INPUT.value*0.000568181818)}
    
    
    //Miles - Miles
    var INPUT = document.getElementById("input");
    if (MILES1.checked && MILES2.checked) {document.write(INPUT.value)}
    
    
    //Miles - Inches
    var INPUT = document.getElementById("input");
    if (MILES1.checked && INCHES2.checked) {document.write(INPUT.value*63 360)}
    
    
    //Miles - Feet
    var INPUT = document.getElementById("input");
    if (MILES1.checked && FEET2.checked) {document.write(INPUT.value*5280)}
    
    
    //Miles - Yard
    var INPUT = document.getElementById("input");
    if (MILES1.checked && YARD2.checked) {document.write(INPUT.value*1760)}
    
    }
    
    </script>
    </head>
    <body>
    <form>
    
    
    <h4>Input Measurement:</h4>
    
    <input type="text" id="input"/>
    
    <input type="radio" name="Type1" id="CM1" checked="true"/>Cm 
    
    <input type="radio" name="Type1" id="KM1" checked="true"/>Km 
    
    <input type="radio" name="Type1" id="METERS1" checked="true"/>Meters 
    
    <input type="radio" name="Type1" id="INCHES1" checked="true"/>Inches 
    
    <input type="radio" name="Type1" id="FEET1" checked="true"/>Feet 
    
    <input type="radio" name="Type1" id="YARD1" checked="true"/>Yard 
    
    <input type="radio" name="Type1" id="MILES1" checked="true"/>Miles
    
    
    
    <h4>Conversion Measurement:</h4>
    
    <input type="radio" name="Type2" id="CM2" checked="true"/>Cm <br/>
     
    <input type="radio" name="Type2" id="KM2" checked="true"/>Km <br/>
      
    <input type="radio" name="Type2" id="METERS2" checked="true"/>Meters <br/>
    
    <input type="radio" name="Type2" id="INCHES2" checked="true"/>Inches <br/>
    
    <input type="radio" name="Type2" id="FEET2" checked="true"/>Feet <br/>
    
    <input type="radio" name="Type2" id="YARD2" checked="true"/>Yard <br/>
    
    <input type="radio" name="Type2" id="MILES2" checked="true"/>Miles <br/>
    
    
    <input type="button" onclick="calculate()" value="Calculate"/>
    
    </form>
    </body>
    </html>

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    There were extra spaces on your script. It existed in at least 4 lines on your script. Find the lines below and remove highlighted:
    Code:
    if (KM1.checked && INCHES2.checked) {document.write(INPUT.value*39_370.0787)}
    .
    .
    .
    if (KM1.checked && YARD2.checked) {document.write(INPUT.value*1_093.6133)}
    .
    .
    .
    if (MILES1.checked && CM2.checked) {document.write(INPUT.value*160_934.4)}
    .
    .
    .
    if (MILES1.checked && INCHES2.checked) {document.write(INPUT.value*63_360)}
    ..the areas highlighted above which are underscores are actually spaces on your end. I did that in order to highlight the issue properly.

    Hope that helps.
    Last edited by rangana; 10-06-2011 at 12:32 AM.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    NoCodeMonkey (10-07-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Rangana: Those extra spaces are often put in by this forum's software, for no reason I've yet to find.

    I don't think that has anything to do with his problem.

    NoCode: You will need to pretty much start over.

    If you use document.write *AFTER* a page is fully loaded, it WIPES OUT the ENTIRE page!!! Even including the javascript code that did the document.write.

    This means that you can NEVER use document.write to respond to any kind of user input or any kind of user-initiated event.

    But there are many other things wrong with your code.

    YOu keep using the IDs of the radio buttons as a means of reaching them. That *ONLY* works in MSIE browsers (and maybe not even then in MSIE 9). In all other browser you would need to use document.getElementById(...) or some other construction.

    You'd be well off just tossing this effort and starting over.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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