Home > Software engineering >  Javascript Variable and HTML
Javascript Variable and HTML

Time:10-05

<tr>
            <td colspan="4" style="height: 26px">
                <asp:Label ID="display1" runat="server"></asp:Label>
            </td>
</tr>
<script type="text/javascript">
                window.onload = function() {
                    var today = new Date();
                    var day = today.getDay();
                    var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
                    var date = today.getFullYear()   '-'   (today.getMonth()   1)   '-'   today.getDate();
                    var time = today.getHours()   ":"   today.getMinutes()   ":"   today.getSeconds();
                    var datetime = date   ' '   time;

                  document.getElementById('display1').innerHTML = datetime   ' <br> Day: '   daylist[day];
                };
</script>

I was wondering how do I display 'display1' in HTML Label? The is the HTML code and my objective is to input the date and time (Javascript) into the label (HTML). I have used "document.getElementById" code but I could not achieve the result that I wanted.

CodePudding user response:

I tried your code in my computer and it's working as it should. Anyway if you want to print there the server date and time you should use the asp.net to create a date time and print directly. Instead if you want the client date and time you can remain on javascript but remove the label and set the id in the td directly

<tr>
 <td colspan="4" id="display1" style="height: 26px">
 </td>
</tr>

CodePudding user response:

It does display it here. using https://codepen.io/

  • Related