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>
a does not have a type.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>