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

146
Views
Not Able to change css using JS

I am working on DJango which also Involves JavaScript I need to change background color of an element after a certain action but am unable to do so. Below code is the only Javascript file used in the project.

function create_email(email){
const element = document.createElement('div');
element.id = "email";
element.innerHTML= `<b>${email.sender}</b>: ${email.subject}<p id='right' ><button id ="view" 
onclick="view_mail(${email.id})"class="btn btn-primary">View</button>${email.timestamp}<button 
id="archive" onclick="archive(${email.id})" class="btn btn-secondary">Archive</button><p>`;
element.addEventListener('click', function() {
console.log(`This message is sent by ${email.sender}`)
});
document.querySelector('#emails-view').append(element);
}

Above I created an Element div using Javascript and assign id email , Later I will be changing this elements css property:-

function read(id){
fetch(`/emails/${id}`, {
method: 'PUT',
body: JSON.stringify({
    read: true
})
})
document.getElementById("email").style.backgroundColor="red";
}

In this function I tried changing the background color of the element but is not changing anything. Note that this function is running as I have tried changing other elements property in this function only and they worked. The element email div is within the div with id emails-view . And I am able to change the background of emails-view div but not email div that is within this div block.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you need to change css in view_mail function:

const email = {
  id: 1,
  sender: 'John',
  subject: 'Message',
  timestamp: '13/09/2021'
}
function create_email(email){
  const element = document.createElement('div');
  element.id = "email";
  element.innerHTML = `<b>${email.sender}</b>: ${email.subject}
  <p id='right'>
  <button id ="view" onclick="view_mail(${email.id})"class="btn btn-primary">View</button>${email.timestamp}
  <button id="archive" onclick="archive(${email.id})" class="btn btn-secondary">Archive</button>
  <p>`;
  element.addEventListener('click', function() {
    console.log(`This message is sent by ${email.sender}`)
  });
  document.querySelector('#emails-view').append(element);
}
function view_mail(id) {
  document.getElementById("email").style.backgroundColor="red";
}

create_email(email)
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">

<div id="emails-view"></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!