Ok, I want to be able to display the a 12hr clock, or 24 hr clock depending on which radio button is selected, but I cannot get the code to work. Please help. My code:
Code:<html> <head> <title>Assignment 9c Clock </title> </head> <body> <script type="text/javascript"> var disp_field = document.getElementById("display"); var radios = document.getElementsByName("format"); var ampm; var format; var time; function run_clock(){ date_inst = new Date(); var hours = date_inst.getHours(); var mins = date_inst.getMinutes(); var seconds = date_inst.getSeconds(); if (radios[0].checked){ format = "12hour"; if (hours > 12){ hours -= 12; ampm = "P.M"; } else{ ampm = "A.M"; } } else{ format = "24hour"; } if (seconds < 10){ seconds = "0" + seconds; } if (format == "12hour"){ time = hours+":"+mins+":"+seconds+" "+ampm; } else{ time = hours+":"+mins+":"+seconds; } disp_field.value = time; } var repeat = window.setInterval("run_clock()",1000); </script> <div id="display"></div> <form> <input type="radio" id="format" name="format">format</input> <input type="radio" id="format" name="format">format</input> </form> </body> </html>


Reply With Quote

Bookmarks