I have a problem resetting the content for the divs in the view where I show a single email. For some reason the divs console.log out as "null", while the parent element for some reason works to console.log. Can somebody help me understand why it don't work?
function show_email(id) {
// Show the mailbox and hide other views
document.querySelector('#email-view').style.display = 'block';
document.querySelector('#emails-view').style.display = 'none';
document.querySelector('#compose-view').style.display = 'none';
// THESE DON'T WORK AS THEY DISPLAY AS "NULL"
//document.querySelector('#read-timestamp').innerHTML = ""
//document.querySelector('#read-sender').innerHTML = ""
//document.querySelector('#read-recipients').innerHTML = ""
//document.querySelector('#read-body').innerHTML = ""
fetch(`/emails/${id}`)
.then(response => response.json())
.then(email => {
subject = email.subject
sender = email.sender
recipients = email.recipients
timestamp = email.timestamp
body = email.body
// Create div for each mail
let element = document.createElement("div");
element.setAttribute("id", "element")
element.classList.add("mailcontainer")
// Create a top container for title and time
let topcontainer = document.createElement("div")
topcontainer.classList.add("topcontainer")
element.appendChild(topcontainer)
// Create a top container for title and time
let subjectp = document.createElement("p")
subjectp.classList.add("subject")
subjectp.setAttribute("id", "read-subject")
subjectp.innerHTML = subject
topcontainer.appendChild(subjectp)
// Create a top container for title and time
let timestampp = document.createElement("p")
timestampp.classList.add("timestamp")
subjectp.setAttribute("id", "read-timestamp")
timestampp.innerHTML = timestamp
topcontainer.appendChild(timestampp)
// Create a top container for title and time
let senderp = document.createElement("p")
senderp.classList.add("sender")
subjectp.setAttribute("id", "read-sender")
senderp.innerHTML = `From: ${sender}`
element.appendChild(senderp)
// Create a top container for title and time
let recipientp = document.createElement("p")
recipientp.classList.add("recipients")
subjectp.setAttribute("id", "read-recipients")
recipientp.innerHTML = `To: ${recipients}`
element.appendChild(recipientp)
// Create a top container for title and time
let bodyp = document.createElement("p")
bodyp.classList.add("bodyp")
subjectp.setAttribute("id", "read-body")
bodyp.innerHTML = body
element.appendChild(bodyp)
// Append the whole element to the email view
document.querySelector("#email-view").append(element)
let elements = document.querySelector("#element")
// THIS ACTUALLY PRINT OUT THE ELEMT
console.log(elements)
// BUT THIS PRINT OUT AS "NULL"
let recipientss = document.querySelector("#read-recipients")
console.log(recipientss)
})
}
HTML
{% extends "mail/layout.html" %}
{% load static %}
{% block body %}
<h2>{{ request.user.email }}</h2>
<button class="btn btn-sm btn-outline-primary" id="inbox">Inbox</button>
<button class="btn btn-sm btn-outline-primary" id="compose">Compose</button>
<button class="btn btn-sm btn-outline-primary" id="sent">Sent</button>
<button class="btn btn-sm btn-outline-primary" id="archived">Archived</button>
<a class="btn btn-sm btn-outline-primary" href="{% url 'logout' %}">Log Out</a>
<hr>
<div id="emails-view">
</div>
<div id="email-view">
</div>
<div id="compose-view">
<h3>New Email</h3>
<form id="compose-form">
<div class="form-group">
From: <input disabled class="form-control" value="{{ request.user.email }}">
</div>
<div class="form-group">
To: <input id="compose-recipients" class="form-control">
</div>
<div class="form-group">
<input class="form-control" id="compose-subject" placeholder="Subject">
</div>
<textarea class="form-control" id="compose-body" placeholder="Body"></textarea>
<button id="submit" type="button" class="btn btn-primary"/>Submit</button>
</form>
</div>
{% endblock %}
{% block script %}
<script src="{% static 'mail/inbox.js' %}"></script>
{% endblock %}