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

194
Vistas
How to select/unselect multiple CheckBoxes by specific checkbox names on Button Click

What would be an easy way to select multiple checkboxes (not all) with a click of a button?

Any suggestion on how to make this easily?

Thanks, EraNet

function Select() {
  var items = document.getElementsByName('Red')
  for (var i = 0; i < items.length; i++) {
    if (items[i].type == 'checkbox')
      items[i].checked = true;
  }

}

function UnSelect() {
  var items = document.getElementsByName('Red')
  for (var i = 0; i < items.length; i++) {
    if (items[i].type == 'checkbox')
      items[i].checked = false;
  }
}
<input type="checkbox" name="Red" value="Red">Red<br>
<input type="checkbox" name="Blue" value="Blue">Blue<br>
<input type="checkbox" name="Yellow" value="Yellow">Yellow<br>
<input type="checkbox" name="Orange" value="Orange">Orange<br>
<input type="checkbox" name="Pink" value="Pink">Pink<br>
<input type="checkbox" name="Voilet" value="Voilet">Voilet<br>
<p>
  <input type="button" onclick='Select()' value="Select" />
  <input type="button" onclick='UnSelect()' value="Unselect" />
</p>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I added a class name for selecting some/all and then using querySelectorAll() to select the elements that need change.

Update

I extended the example with more "select buttons", so that you can see that querySelectorAll() is flexible in the input elements that you would like to update.

function Select(str) {
  UnSelect();
  // all input elements that has the class name "one"
  var items = document.querySelectorAll(str);
  items.forEach(item => item.checked = true);
}

function UnSelect() {
  // all input elements that has a name
  var items = document.querySelectorAll('input[name]');
  items.forEach(item => item.checked = false);
}
<input type="checkbox" class="one" name="Red" value="Red">Red<br>
<input type="checkbox" class="one" name="Blue" value="Blue">Blue<br>
<input type="checkbox" class="one three" name="Yellow" value="Yellow">Yellow<br>
<input type="checkbox" class="two three" name="Orange" value="Orange">Orange<br>
<input type="checkbox" class="two" name="Pink" value="Pink">Pink<br>
<input type="checkbox" class="two" name="Voilet" value="Voilet">Voilet<br>
<p>
  <input type="button" onclick="Select('input.one')" value="Select by classname 'one'" />
  <input type="button" onclick="Select(`input[name = 'Blue']`)" value="Select by name 'Blue'" />
  <input type="button" onclick="Select(`input[name = 'Blue'],input[name = 'Pink']`)" value="Select by name 'Blue' and 'Pink'" />
  <input type="button" onclick="Select(`input.three`)" value="Select by classname 'three'" />
  <input type="button" onclick='UnSelect()' value="Unselect" />
</p>

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