iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
☑️

Bookmarklet for Bulk Checking Checkboxes

に公開

This is a bookmarklet that, when executed, sets all checkboxes to the opposite state of the first checkbox.

// Toggle all checkboxes
javascript: (() => {
  const inputs = Array.from(document.querySelectorAll('input[type=checkbox]'))
  if (inputs.length === 0) return

  const checked = !inputs[0].checked

  for (const input of inputs) {
    input.checked = checked
  }
})()

The Array.from(document.querySelectorAll('')) part is a tip I often use.
Since the return value of document.querySelectorAll is a NodeList type and can be difficult to use in loops, I convert it into an Array.

Supplement: Bookmarklet Conventions

javascript: (() => {
})()

When executing JavaScript from a hyperlink, use the javascript: labeled statement.
Wrap it in an immediately invoked function expression to avoid polluting the global scope.

GitHubで編集を提案

Discussion