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

118
Vistas
how to run JavaScript code when checkbox is checked

There is a button that passes value to another input field. but now I want to pass value if checkbox is checked.

const copy = (id) => {
  var value = document.getElementById("col" + id + "-input").value
  var list = document.getElementsByClassName("col" + id + "-input")
  for (i = 0; i < list.length; i++)
    list[i].value = value
}

document.getElementById("col1-button").addEventListener("click", () => copy(1))
<a type="button" id="col1-button">OK</a>

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

  1. a does not have a type.
  2. Pass the number in a data-attribute then you can reuse the script for more than one button

I guessed the HTML. Next time post a complete example please

const copy = e => {
  const tgt = e.target; 
  if (tgt.type !=="checkbox" || !tgt.checked) return;
  const id = e.target.dataset.id;
  var value = document.getElementById("col" + id + "-input").value
  document.querySelectorAll(".col" + id + "-input").forEach(inp => inp.value = value);
};

document.getElementById("col1-button").addEventListener("click",copy)
<input type="text" id="col1-input" />
<input type="text" class="col1-input" />
<input type="text" class="col1-input" />
<input type="text" class="col1-input" />
<label><input type="checkbox" id="col1-button" data-id="1" />OK</label>

about 4 years ago · Santiago Gelvez Denunciar
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