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

214
Visualizações
Why is my font size not adjusting with vanilla js?

I have this little code here and by the console logs I can see it is apparently changed but on the display it is not changing. What could be going wrong ?

JS vanilla below

const gameItem = document.querySelectorAll('.game-item');
const gameItemS = document.querySelector('.game-item');

document.addEventListener('DOMContentLoaded', () => {
    titleSize(gameItem);
});

const titleSize = (gameItem) => {
    [...gameItem].forEach((title) => {
        let textSize = window
            .getComputedStyle(title, null)
            .getPropertyValue('font-size');
        let gameTitle = title.innerText;
        // console.log(gameTitle, gameTitle.length, textSize);
        if (gameTitle.length > 7) {
            textSize = '5px';
            console.log(
                'The font on this was changed: ' + gameTitle,
                gameTitle.length,
                textSize
            );
        }
    });
};

html below

            <section class="games">
                <h1>Games</h1>
                <ol class="container">
                    <li class="game-item">Halo<img src="" /></li>
                    <li class="game-item">Pokemon<img src="" /></li>
                    <li class="game-item">
                        Animal Crossing<img src="" />
                    </li>
                    <li class="game-item">Genshin Impact<img src="" /></li>
                    <li class="game-item">Skyrim<img src="" /></li>
                </ol>
            </section>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Assigning to textSize just changes the value of that variable, it doesn't have any effect on the element. When you do let textSize = /*...*/.getPropertyValue('font-size');, that just copies the value from the computed style object into the variable. There's no ongoing link between the variable and that computed style object. (Even if there were, assigning to the computed style object won't change the element.)

To change the element's style, you'd have to assign to title.style.fontSize (or title.style["font-size"]):

const gameItem = document.querySelectorAll('.game-item');
const gameItemS = document.querySelector('.game-item');

document.addEventListener('DOMContentLoaded', () => {
    titleSize(gameItem);
});

const titleSize = (gameItem) => {
    [...gameItem].forEach((title) => {
        let textSize = window
            .getComputedStyle(title, null)
            .getPropertyValue('font-size');
        let gameTitle = title.innerText;
        // console.log(gameTitle, gameTitle.length, textSize);
        if (gameTitle.length > 7) {
            title.style.fontSize = '5px';
            console.log(
                'The font on this was changed: ' + gameTitle,
                gameTitle.length,
                textSize
            );
        }
    });
};
<section class="games">
    <h1>Games</h1>
    <ol class="container">
        <li class="game-item">Halo<img src="" /></li>
        <li class="game-item">Pokemon<img src="" /></li>
        <li class="game-item">
            Animal Crossing<img src="" />
        </li>
        <li class="game-item">Genshin Impact<img src="" /></li>
        <li class="game-item">Skyrim<img src="" /></li>
    </ol>
</section>

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