0

I am new to HTML and am facing difficulties in a task I am currently working on.

I am to display the current date in a format like this : "7 October 2022 Friday 10:47" in a non-editable textbox. The date, time etc. is supposed to change as the user refreshes the page.

For example, when a user refreshes the page after one minute it should display "7 October 2022 Friday 10:48" and if the user accesses the page on the next day at the same time it should display "8 October 2022 Saturday 10:47".

Here's what I did for the textbox:

<label for = "date-time"> Date & Time:</label>
<input type ="read only" name= "date-time" id = "date" readonly>

And this is the in line Javascript that I have done:

<script>
function getDate()
{
        var todayDate = new Date();
        document.getElementById("date")= todayDate;
}
</script>

Instead when I run this code, I get an empty non-editable textbox. I have tried to search up what is wrong but I don't seem to find my issue.

My <script> is also placed at the end of <body>, so I am unsure of what I am doing wrong.

EDIT: Updated code that displays it!

<label for = "date-time"> Date & Time:</label>
<input type ="text" id = "date" readonly>
<script>
function getDate()
{
        var todayDate = new Date();
        document.getElementById("date").value= todayDate;
}
</script>
Malcolm
  • 9
  • 3
  • The problem is that you are trying to assign to a DOM element, rather than changing the text it contains. Instead do: `document.getElementById("date").value = todayDate;` – akaAbdullahMateen Oct 21 '22 at 18:27
  • @Jesse I'm sorry and will avoid this in the future. Thank you. – Malcolm Oct 21 '22 at 18:31
  • @Malcolm You do not need to apologize. Your question was well-written; over look happen when coding. I hope you found the solution you came for. In case, you still have issue, you are more than welcome to ask here :) – akaAbdullahMateen Oct 21 '22 at 18:34
  • 1
    @akaAbdullahMateen Yes I indeed have found my solution. Thanks! – Malcolm Oct 21 '22 at 18:35

0 Answers0