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

156
Vistas
Changing element style using JavaScript

I am trying to change a spans style using JavaScript but cannot seem to do it. Below I have created the span and given it an Id "spanzo", then I have stored the span in a var "elementtl" and tried two ways to add styles to it but its not doing anything. Styles have already been set to the span, I am just trying to override them styles.

const letterEl = document.createElement("span");
    letterEl.setAttribute("id", "spanzo");

var elementtl = document.getElementById("spanzo");
    elementtl.classList.toggle("spanzo1");
    elementtl.style.backgroundColor = "red"; 


#spanzo{
transform: rotatey(0deg);
}
.spanzo1{
transform: rotatey(180deg);
transition: 2s;
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const letterEl = document.createElement("span");
    letterEl.setAttribute("id", "spanzo");
    letterEl.textContent = 'hello world';
    document.body.appendChild(letterEl);


var elementtl = document.getElementById("spanzo");
    elementtl.classList.toggle("spanzo1");
    elementtl.style.backgroundColor = "red"; 
#spanzo{
transform: rotatey(0deg);
}
.spanzo1{
transform: rotatey(180deg);
transition: 2s;
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

First of all you need to add a width and height to your #spanzo style for it to actually occupy some space and you need to add it to the <body> element using

document.body.append(letterEl)

All together

const letterEl = document.createElement("span");
    letterEl.setAttribute("id", "spanzo");
    
  document.body.append(letterEl)  
document.body.appendChild(letterEl);

var elementtl = document.getElementById("spanzo");
    elementtl.classList.toggle("spanzo1");
    elementtl.style.backgroundColor = "red"; 

#spanzo{
transform: rotatey(0deg);
width:50px;
height:50px;
}
.spanzo1{
transform: rotatey(180deg);
transition: 2s;
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

const letterEl = document.createElement("span");
letterEl.innerText = "This is a span.";
document.body.appendChild(letterEl);
    letterEl.setAttribute("id", "spanzo");

var elementtl = document.getElementById("spanzo");
    elementtl.classList.toggle("spanzo1");
    elementtl.style.backgroundColor = "red"; 
#spanzo{
transform: rotatey(0deg);
}
.spanzo1{
transform: rotatey(180deg);
transition: 2s;
}

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