Hi all, very new to Javascript, been searching the net for a simple script which has a text box with a button beside and when the visitor clicks the button the date is enter in to the text box automaticly.
<input type = "button" value = "Get today's date" onclick = "getTheDate()">
<input type = "text" id = "todayDate" readonly">
<script type = "text/javascript">
function getTheDate() {
document.getElementById("todayDate").value = new Date().toDateString();
}
</script>
Quizmaster: Which ancient Greek poet is also the first name of a main character in "The Simpsons"?
Contestant: Bart
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Better if you ask for what you want at the outset.
Code:
<input type = "button" value = "Get today's date" onclick = "getTheDate()">
<input type = "text" id = "todayDate" readonly">
<script type = "text/javascript">
function getTheDate() {
var today = new Date() ;
var yr = today.getFullYear();
var mth = today.getMonth()+1; // months in Javascript are 0-11
if (mth <10) {mth = "0" + mth}
var dt = today.getDate();
if (dt <10) {dt = "0" + dt}
// you can now display the date in any format you require
var theDate = yr + "-" + mth + "-" + dt;
document.getElementById("todayDate").value = theDate;
}
</script>
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.