Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
font awesome icon not reacting to event listener Javascript

I am trying to create an event listener that uses PUT request (Fetch) to change a parameter 'Archive' to true or false when the font-awesome icon is clicked however, it's not working (not even the console log is being fired).

How can I fix this?

//create function to confirm if email is archived (INBOX ONLY)
function confirmArchiveStatus() {
  let archiveBtn = document.querySelector('.archive')
  //ADDeventlistener for click to archive button
  archiveBtn.addEventListener('click', function() {
    //just testing if this fired 
    console.log('archived: true');
    //use put request to mark email as achived
    fetch(`/emails/inbox/${id}`, {
      method: 'PUT',
      body: JSON.stringify({
          archived: true,        
      })
    })
    //Load inbox to refresh archived mails out 
    load_mailbox('inbox');
  });

    archiveBtn.addEventListener('click', function() {
    console.log('archived: false');
      //if archive is true, add event listener click to remove from archived mails
        if(email.archived != true) {
          fetch(`/emails/archive/${id}`, {
            method: 'PUT',
            body: JSON.stringify({
                archived: false,
            })
          })
      }
    //Load inbox to refresh archived mails in 
    load_mailbox('inbox');
  });
}

FONT AWESOME LINK

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

HTML

                    <div class="actions">
                        <span class="action archive"><i class="fa fa-archive"></i></span>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should clarify a function declaration/definition versus a function call.

//create function to confirm if email is archived (INBOX ONLY)
// THIS IS A FUNCTION DECLARATION that sets up an event listener (twice, 
// not sure you want to do this twice) but these event listeners
// are NEVER actually set until...(see below):

function confirmArchiveStatus() {
  let archiveBtn = document.querySelector('.archive');

  //ADDeventlistener for click to archive button
  archiveBtn.addEventListener('click', function() {
    console.log('archived: true');
  });

  archiveBtn.addEventListener('click', function() {
    console.log('archived: false');
  });
}


//THIS IS A FUNCTION CALL:
confirmArchiveStatus();
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="actions">
  <span class="action archive"><i class="fa fa-archive"></i></span>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!