0

I'm not sure why the date won't display in this django template, using javascript and html: I've also found this helpful: how to display a javascript var in html body

But following that, still can't get it to work. Currently I have:

{% extends 'chattr.html' %}

<section class="">
    hello
</section>
    <script>
        let d = new Date();

        document.getElementById("date0").innerHTML = d;
        n =  new Date();
        y = n.getFullYear();
        m = n.getMonth() + 1;
        d = n.getDate();
        document.getElementById("date").innerHTML = m + "/" + d + "/" + y;
    </script>
<p id="date"></p>
{% block chatStream %}
<b>smallest_steps:</b> <br> 
Hello! Please send me a message:)   <span id="date0"></span>  <br>
<p id="date"></p>
{% endblock %}

and chattr.html

<h1>ss</h1>
{% block title %}{% endblock %}
{% block chatStream %} {% endblock %}

Thanks so much

Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • 2
    Your ` – Willem Van Onsem Nov 22 '21 at 19:20
  • @WillemVanOnsem thanks so much -- that makes a ton of sense:) – Katie Melosto Nov 22 '21 at 19:23
  • @WillemVanOnsem thanks again... I moved it into the block, but I'm getting this error now: ```Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at (index):7```... I'm not sure why the date object would be null... – Katie Melosto Nov 22 '21 at 19:30
  • I think you will need to wrap it in a function that is evaluated if the entire body is loaded, for example wth jQuery through `$(function () { ... })`: https://learn.jquery.com/using-jquery-core/document-ready/ – Willem Van Onsem Nov 22 '21 at 19:32

0 Answers0