Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda