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

200
Visualizações
VS Code Intellisense for JavaScript is not working?

I'm trying to loop over a bunch of buttons to give each of them an event listener that will change the background color once I click any of them, but vs code's IntelliSense won't autocomplete the desired property I'm looking for such .style .

code snippit

const buttons = document.querySelectorAll('btn');

for(let button of buttons) {
    button.addEventListener('click', () => {
        button.*style.backgroundColor* = 'red';
    })
}

HTML

<body>
    <section id="container">
        <button class="btn">Click me</button>
        <button class="btn">Click me</button>
        <button class="btn">Click me</button>
        <button class="btn">Click me</button>
    </section>
    <script src="app.js"></script>
</body>

VS Code intellisense won't recommend the italicized code .style and .backgroundColor

it's happening of both block and scope level as will as global level.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is that document.querySelectorAll is specified to give you a NodeList containing Element objects, a general base class. Thus each button is, as far as the JavaScript IntelliSense can infer, merely an instance of Element. But the style property exists on HTMLElement. Therefore you need to 'tell' the IntelliSense engine that buttons is going to be a NodeList of HTMLElement instead, which you can accomplish with a JSDoc:

/** @type {NodeListOf<HTMLElement>} */
const buttons = document.querySelectorAll('btn');

for(let button of buttons) {
    button.addEventListener('click', () => {
        button.style.backgroundColor = 'red';
    })
}
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