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

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

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 Relatório

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 Relatório

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 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