Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda