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

138
Visualizações
Can var elements' be group together for styling on JavaScript?

I am trying to change the styling for my HTML elements without touching the HTML and CSS file.

        resultFirstName.style.fontWeight = "bold";
        resultFirstName.style.fontStyle = "uppercase";
        resultFirstName.style.fontStyle = "italic";
        resultFirstName.style.fontSize = "1.5em";

        resultLastName.style.fontWeight = "bold";
        resultLastName.style.fontStyle = "uppercase";
        resultLastName.style.fontStyle = "italic";
        resultLastName.style.fontSize = "1.5em";

I am trying to put these two variables together into something like this below. But I am not sure how to do it.

        resultFirstName + resultLastName.style.fontWeight
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can create a string variable with all the css properties and values you want, and assign that to the selected element. PS for uppercase use text-transform 🙂

const styles = `
    font-size: 1.5em;
    font-style: italic;
    font-weight: bold;
    text-transform: uppercase;`

document.querySelector('.firtsName').style = styles
<h1 class='firtsName'> Test </h1>

about 4 years ago · Juan Pablo Isaza Relatório

0

You could do the following:

const styles = {
    fontWeight: "bold",
    textTransform: "uppercase",
    fontStyle: "italic",
    fontSize: "1.5em"
};

Object.assign(resultFirstName.style, styles);
Object.assign(resultLastName.style, styles);

Using Object.assign will just add/update the given declarations. Other existing declarations are left untouched.

If you want to reset all styles such that only your ones are set for the element, you can just clear them before assigning new ones:

resultFirstName.style.cssText = '';
resultLastName.style.cssText = '';

Side note: I have changed the property used for the uppercase value from fontStyle to textTransform because probably that's what you actually meant.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a for loop like this:

const elements = [resultFirstName, resultLastName];

for (const element of elements) {
    element.style.fontWeight = "bold";
    element.style.textTransform = "uppercase";
    element.style.fontStyle = "italic";
    element.style.fontSize = "1.5em";
}

Another possible way is:

const elements = [resultFirstName, resultLastName];

for (var i = 0; i < elements.length; ++i) {
    const element = elements[i];

    element.style.fontWeight = "bold";
    element.style.textTransform = "uppercase";
    element.style.fontStyle = "italic";
    element.style.fontSize = "1.5em";
}
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