Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!