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

81
Visualizações
Get checked checkboxes trigger event on sibling elements

I have these buttons and want to click the selected ones at one time

<div class="form-group">
  <input type="checkbox">
  <label><button onclick="alert('a')">button1</button></label>
</div>
<div class="form-group">
  <input type="checkbox">
  <label><button onclick="alert('b')">button2</button></label>
</div>
<div class="form-group">
  <input type="checkbox">
  <label><button onclick="alert('c')">button3</button></label>
</div>
<button>go</button>

I tried some jquery stuff but not made it

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

0

Instead of just giving you some code:

  • You're not assigning any click to your GO button (Why?)
  • Add an ID to your GO button to help you query it in JS
  • Assign a class to your checkboxes as well. Like: class="chk"
  • Don't use unsafe inline on* handlers. JS should be in one place only, and that's its respective tag or file. Use addEventListener instead
  • Makes no sense to use <button> inside <label> since it will steal the focus.
  • Your checkboxes are missing the value attribute. Use that attribute to store the desired value.
  • You're missing for attributes. Either add id to your INPUTs and for to your labels, or just wrap everything inside the LABEL
  • You're missing type="button" on your buttons
  • Avoid the use of prompt, alert. Replace those with a proper UI. For testing purpose use console

// DOM utility functions:

const EL = (sel, par) => (par || document).querySelector(sel);
const ELS = (sel, par) => (par || document).querySelectorAll(sel);

// Task:

const doSomething = (EL_chk) => {
  // Do something...
  console.log(EL_chk.value);
};

// Add change events to each checkbox
ELS(".chk").forEach((EL_chk) => {
   EL_chk.addEventListener("input", () => doSomething(EL_chk));
});

// On button GO click, target the checked ones
EL("#go").addEventListener("click", () => {
  ELS(".chk:checked").forEach(doSomething);
});
.btn {
  display: inline-block;
  background: #eee;
  padding: 0.3rem 0.5rem;
  border-radius: 0.2rem;
  border: 0;
}
<div class="form-group">
  <label>
    <input class="chk" type="checkbox" value="a">
    <!-- DON'T USE <button> INSIDE A LABEL -->
    <span class="btn">button A</span>
  </label>
</div>

<div class="form-group">
  <label>
    <input class="chk" type="checkbox" value="b">
    <span class="btn" data-click="a">button B</span>
  </label>
</div>

<div class="form-group">
  <label>
    <input class="chk" type="checkbox" value="c">
    <span class="btn">button C</span>
  </label>
</div>

<button id="go" class="btn" type="button">GO</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this. In JS we are selected button with checkbox status checked and then triggering click event.

function go() {
  document.querySelectorAll('input[type="checkbox"]:checked ~ label > button').forEach(function(element){
    element.click();
  })
}
<div class="form-group">
   <input type="checkbox" >
   <label ><button onclick="alert('a')">button1</button></label>
</div>
<div class="form-group">
   <input type="checkbox" >
   <label ><button onclick="alert('b')">button2</button></label>
</div>
<div class="form-group">
   <input type="checkbox" >
   <label ><button onclick="alert('c')">button3</button></label>
</div>
<br>
<button onclick="go()">go</button>

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