Hi

I want to create a 'select all' checkbox or button associated with a checkbox group. This is so that the user can automatically select all checkboxes in the group as there is a long list on the form.

Im sure there is a simple way to do this but Im am a real web design beginner..

Thanks in advance for your response

Jo

Dani AI

Generated

As describes, a single control to toggle a long list is the right idea. ’s suggestion to loop rows can work, and / correctly point toward a client-side solution. The gaps most beginners hit are accessibility, correct scoping (only target the checkboxes you mean), and handling the “partial selection” state — below is a compact, modern pattern that covers those points.

Use semantic markup (a fieldset/legend or a container) and put label elements around each checkbox so clicks and keyboard users work predictably. Put the “Select all” checkbox outside the repeated items (top and optionally bottom for long lists). Give item checkboxes a class or data attribute so your script only touches the intended inputs.

<label><input id="select-all" type="checkbox"> Select all</label>

<fieldset id="group">
  <label><input class="item" type="checkbox" name="items[]" value="1"> One</label>
  <label><input class="item" type="checkbox" name="items[]" value="2"> Two</label>
  <!-- more -->
</fieldset>

<script>
const selectAll = document.getElementById('select-all');
const group = document.getElementById('group');

selectAll.addEventListener('change', () => {
  const checked = selectAll.checked;
  group.querySelectorAll('input[type="checkbox"].item')
    .forEach(cb => cb.checked = checked);
  selectAll.indeterminate = false;
  selectAll.setAttribute('aria-checked', checked ? 'true' : 'false');
});

group.addEventListener('change', () => {
  const boxes = Array.from(group.querySelectorAll('input[type="checkbox"].item'));
  const checkedCount = boxes.filter(b => b.checked).length;
  if (checkedCount === 0) {
    selectAll.checked = false; selectAll.indeterminate = false;
  } else if (checkedCount === boxes.length) {
    selectAll.checked = true; selectAll.indeterminate = false;
  } else {
    selectAll.indeterminate = true;
    selectAll.setAttribute('aria-checked', 'mixed');
  }
});
</script>

Troubleshooting tips: ensure the .item selector matches your inputs; test keyboard and a screen reader (some assistive tech reads the indeterminate state better if you also set aria-checked="mixed" as shown). For very large datasets, consider a server-side “apply to all matching items” flag instead of toggling thousands of DOM nodes to avoid performance and UX issues. If JavaScript may be disabled, include a noscript note or a server fallback.

Recommended Answers

All 3 Replies

What language are you using? any way you can use a table to contain all checkboxes and use loop to access each row containing checkbox to access them

A little googleing on javascript snippets for checking form boxes landed me here (1st link for this query: "javascript 'check all' boxes in form"

http://www.somacon.com/p117.php

There are any number of ways to implement this, but imnsho, javascript is going to be the most cross-browser friendly.

HTH,
-Ray

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.