'Button not showing when using JS to dynamically add to page

function addElements(mailbox) {
  if (mailbox != 'sent') {
    let actionDivArchiveBtn = document.querySelector('#actions')
    let archiveButton = document.createElement("button");
    archiveButton.classList.add('archive');
    archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
    actionDivArchiveBtn.appendChild(archiveButton);

    archiveButton.addEventListener('click', () => {
      if (email.archived === false) {
        actionDivArchiveBtn.style.color = "blue";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: true
          })
        })
      } else {
        actionDivArchiveBtn.style.color = "black";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: false
          })
        })
      }
    })
  }
  load_mailbox('inbox');
}
<div class="actions">
  <span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
  <span class="from">${email.sender}</span>
  <span class="date">
                            <span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>

I am creating a gmail like single page application, I want to create an archive button dynamically using vanilla javascript (the button is not showing on my screen) and I want to listen to when the user clicks on the archive button using event listeners then check if archive is true using PUT and change to false and vice versa.

The problem I am having is that my button is not showing.

I don't know why because I've checked similar examples of how to to create and append buttons and similar process was taken. Please help with a code review.


                        <div class="actions">
                            <span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
                        </div>
                        <div class="header">
                            <span class="from">${email.sender}</span>
                            <span class="date">
                            <span class="float-right"></span>${email.timestamp}</span>
                        </div>
                        <div class="title">${email.subject}</div>
                        <div class="description">${email.body}</div>

function addElements(mailbox) {
      if (mailbox != 'sent'){
        let actionDivArchiveBtn = document.querySelector('#actions')
        let archiveButton = document.createElement("button");
        archiveButton.classList.add('archive');
        archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
        actionDivArchiveBtn.appendChild(archiveButton);

        archiveButton.addEventListener('click',() => {
          if (email.archived === false) {

function addElements(mailbox) {
  if (mailbox != 'sent') {
    let actionDivArchiveBtn = document.querySelector('#actions')
    let archiveButton = document.createElement("button");
    archiveButton.classList.add('archive');
    archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
    actionDivArchiveBtn.appendChild(archiveButton);

    archiveButton.addEventListener('click', () => {
      if (email.archived === false) {
        actionDivArchiveBtn.style.color = "blue";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: true
          })
        })
      } else {
        actionDivArchiveBtn.style.color = "black";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: false
          })
        })
      }
    })
  }
  load_mailbox('inbox');
}
<div class="actions">
  <span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
  <span class="from">${email.sender}</span>
  <span class="date">
                            <span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>
function addElements(mailbox) {
  if (mailbox != 'sent') {
    let actionDivArchiveBtn = document.querySelector('#actions')
    let archiveButton = document.createElement("button");
    archiveButton.classList.add('archive');
    archiveButton.innerHTML = `<i class="fa fa-archive" aria-hidden="true"></i>`
    actionDivArchiveBtn.appendChild(archiveButton);

    archiveButton.addEventListener('click', () => {
      if (email.archived === false) {
        actionDivArchiveBtn.style.color = "blue";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: true
          })
        })
      } else {
        actionDivArchiveBtn.style.color = "black";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: false
          })
        })
      }
    })
  }
  load_mailbox('inbox');
}
<div class="actions">
  <span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>
<div class="header">
  <span class="from">${email.sender}</span>
  <span class="date">
                            <span class="float-right"></span>${email.timestamp}</span>
</div>
<div class="title">${email.subject}</div>
<div class="description">${email.body}</div>


Solution 1:[1]

Your querySelector for actionDivArchiveBtn is incorrect. Because you search by id but in layout it is class so I changed in layout class to id and now it works. But may be you need class in layout then change # to . in querySelector:

function load_mailbox(string) {
  console.log(string);
}

function addElements(mailbox) {
  if (mailbox != 'sent') {
    let actionDivArchiveBtn = document.querySelector('#actions')
    let archiveButton = document.createElement("button");
    archiveButton.classList.add('archive');
    archiveButton.innerHTML = `SOME TEXT<i class="fa fa-archive" aria-hidden="true"></i>`
    actionDivArchiveBtn.appendChild(archiveButton);

    archiveButton.addEventListener('click', () => {
      if (email.archived === false) {
        actionDivArchiveBtn.style.color = "blue";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: true
          })
        })
      } else {
        actionDivArchiveBtn.style.color = "black";
        fetch(`/emails/archive/${id}`, {
          method: 'PUT',
          body: JSON.stringify({
            archived: false
          })
        })
      }
    })
  }
  load_mailbox('inbox');
}

addElements('receive');
<div id="actions">
  <span class="action"><i class="fa fa-reply" aria-hidden="true"></i></span>
</div>

<div class="header">
  <span class="from">${email.sender}</span>
  
  <span class="date">
    <span class="float-right"></span>${email.timestamp}
  </span>
</div>

<div class="title">${email.subject}</div>

<div class="description">${email.body}</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EzioMercer