Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  186] [ 2]  / answers: 1 / hits: 19161  / 12 Years ago, sun, december 30, 2012, 12:00:00

here is my code,i want to print date in text box after enter date text using onload event.



<!DOCTYPE html>
<html>
<head>
<script>
function displayDate() {
document.getElementById(fname).value = Date();
}
</script>
</head>
<body onload=displayDate()>
Enter date: <input type=date id=fname readonly />
</body>
</html>

More From » html

 Answers
9

You have to



  • correctly create the Date object (you're missing new)

  • format it according to rfc3339 (ex: 2012/12/30)


Change


document.getElementById("fname").value = Date();

to


var now = new Date();
var formatedDate = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();​
document.getElementById("fname").value = formatedDate;

Demonstration


Note that some browsers accept other formats, but Chrome doesn't, as it complies to the norm.


[#81164] Friday, December 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;