I want a button on my website which when clicked inserts the current time in corresponding textarea.
Please note that i dont want to keep showing teh current time. I want the current time to be inserted in the textbox when the button is clicked.
Here is the code what i tried. Please highlight any errors to make it work correctly:
- Code: Select all
<form name = "myForm">
<input type = "button" value = "Click To Insert Current Time" onclick = "setClock()">
<input type = "text" name = "timeDisplay">
<script type="text/javascript">
function setClock() {
var today = new Date();
var hh = today.getHours();
if (hh <10) {hh = "0" + hh};
var mm = today.getMinutes();
if (mm <10) {mm = "0" + mm};
var ss = today.getSeconds();
if (ss <10) {ss = "0" + ss};
var time = hh + ":" + mm + ":" + ss;
document.myform.timeDisplay.value = time;
}
</script>