To retrieve the value of a checked checkbox in jQuery, query checked checkboxes with :checked and read .val()—use $(selector).filter(":checked").val() (or $(${selector}:checked).val()) for a single checkbox or first checked match, and use .map((_, el) => $(el).val()).get() when multiple checkboxes in a group can be checked at once. Avoid mutating DOM elements or collections while reading their values.


1. Getting the Value of a Single Checked Checkbox (:checked + .val())

Select the checkbox safely using document.getElementById() or a scoped selector, check .prop("checked") (or :checked), and return its .val() (or null when unchecked) with explicit guards:

// Retrieve the value of a single checked checkbox in jQuery, or null if unchecked
function getSingleCheckedCheckboxValue(checkboxId, fallback = null) {
  if (typeof window.jQuery !== "function" || typeof checkboxId !== "string" || checkboxId.trim() === "") {
    return fallback;
  }

  const $ = window.jQuery;
  const rawEl = document.getElementById(checkboxId);
  if (!(rawEl instanceof HTMLInputElement) || rawEl.type !== "checkbox") {
    return fallback;
  }

  const $checkbox = $(rawEl);
  if (!$checkbox.prop("checked")) {
    return fallback;
  }

  const val = $checkbox.val();
  return typeof val === "string" ? val : fallback;
}

// Example usage:
// const termsValue = getSingleCheckedCheckboxValue("accept-terms-checkbox");
// console.log("Checked terms value:", termsValue);
  • Explicit checked check: .prop("checked") confirms the checkbox is actually checked rather than merely existing in the DOM.
  • ID lookup without selector escaping bugs: Using document.getElementById() avoids selector issues with colons, dots, or special characters in IDs.

2. Getting All Checked Values from a Checkbox Group (.map() + .get())

When a checkbox group shares a name attribute (such as name="skills[]" or name="permissions"), filter for checked inputs with :checked and collect their values into a new plain array via .map(...).get():

// Retrieve all checked checkbox values for a named group as a new array
function getCheckedCheckboxGroupValues(groupName, scopeSelector = document) {
  if (typeof window.jQuery !== "function" || typeof groupName !== "string" || groupName.trim() === "") {
    return [];
  }

  const $ = window.jQuery;
  const escapedName = groupName.replace(/["\\]/g, "\\$&");
  const $checked = $(scopeSelector).find(
    `input[type="checkbox"][name="${escapedName}"]:checked`
  );

  return $checked
    .map((_index, el) => $(el).val())
    .get()
    .filter((val) => typeof val === "string");
}

// Example usage:
// const selectedPermissions = getCheckedCheckboxGroupValues("permissions", "#role-form");
// console.log("Selected permissions:", selectedPermissions);
  • Returns all selected values: Unlike .val() on a multi-element jQuery collection (which only returns the first element’s value), .map(...).get() returns every checked value in a standard array.
  • Non-mutating: Produces a brand-new array without modifying any checkboxes or existing arrays.

3. Common Traps to Avoid

Retrieving checked checkbox values in jQuery often runs into a few pitfalls:

  • Calling .val() directly on an unchecked checkbox: $(checkbox).val() returns the checkbox’s value attribute string (often "on" by default) even when unchecked—always check :checked or .prop("checked") first.
  • Using .val() on multiple checked checkboxes expecting an array: On a jQuery collection of multiple elements, .val() only reads the first matched element—use .map(...).get() to collect all checked values.
  • Forgetting an explicit value attribute in HTML: If a <input type="checkbox"> omits value="...", its default .val() in browsers is "on", which may not match your backend or state keys.

TL;DR: Check .prop("checked") or :checked before reading .val() for a single checkbox, and use $(scope).find('input[type="checkbox"][name="..."]:checked').map((_, el) => $(el).val()).get() to collect all checked values in a group.