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

210
Vistas
JSHint warning about addEventListener "`Functions declared within loops referencing an outer scoped variable may lead to confusing semantics"

I am using function that makes a clicked button bold while making its other sibling buttons normal font weight. The function works correctly but I am getting a warning in JSHint saying Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. (gLength, buttonParent). I tried using let instead of var in the code below but the warning is still there. How can I fix the issue that JSHint shows?

function boldButton(buttonParent) {
  buttonParent = buttonParent.querySelectorAll("BUTTON");
  if (buttonParent) {

    for (var g = 0, gLength = buttonParent.length; g < gLength; g++) {
      // set all buttons as normal font before setting a clicked button as bold font:
      buttonParent[g].addEventListener('click', function() {
        for (var r = 0; r < gLength; r++) {
          buttonParent[r].style.fontWeight = "normal";
        }
        // set the clicked button as bold font:
        this.style.fontWeight = "bold";
      }, false);
    }
  }
}

var buttonWrap = document.getElementById("button-wrap");
boldButton(buttonWrap);
<div id="button-wrap">
  <button>Button 1</button>
  <button>Button 2</button>
  <button>Button 3</button>
</div>

about 4 years ago · Juan Pablo Isaza
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