NRGdallas
NRGdallas

Reputation: 395

select every checkbox on an entire page

assuming the following code:

<input type="checkbox" name="categories[9507]">

is there a javascript command that can simply select every checkbox like that on the page? the name's change slightly in numbers only.

I need to select around 15000 on a page, without a select all available :(

(something that I could put into a bookmark and click, or console of firebug would be best)

Upvotes: 1

Views: 189

Answers (4)

RobG
RobG

Reputation: 147363

In plain script for any browser since IE 4:

function checkAllCheckboxes(root) {
  root = root || document;
  var inputs = root.getElementsByTagName('input');
  for (var i=0, iLen=inputs.length; i<iLen; i++) {
    if (inputs[i].type == 'checkbox') inputs[i].checked = true;
  }
}

root can be any element containing the checkboxes in question or nothing for the entire document. A regular expression test can be added to check only those whose name starts with "categories", e.g.

  var re = /^categories/;
  var input;
    ...
      input = inputs[i];
    if (input.type == 'checkbox' && re.test(input.name)) {
      input.checked = true;
    }
  ...

As a one line bookmark:

javascript:var e,es=document.getElementsByTagName('input'),i=0,iL=es.length,r=/^categories/;for(;i<iL;){e=es[i++];if(e.type=="checkbox"&&r.test(e.name))e.checked=true}void 0;

Edit

Made bookmark shorter. A few more characters can be removed if required.

Upvotes: 2

Fabr&#237;cio Matt&#233;
Fabr&#237;cio Matt&#233;

Reputation: 70139

Pure JS, modern browsers-only (IE 8+, FF 3.5+, Opera 10+, Safari 3.2+, Chrome 1+):

(function() {
    var chs = document.querySelectorAll​('input[type="checkbox"][name^="categories"]')​​​​​​​​​​​​​​​​​​​​​​​​​​​​;
    for (var i=0; i<chs.length; i++) chs[i].checked = true;
}());

Fiddle

QSA Reference

Upvotes: 4

Luke
Luke

Reputation: 2063

$("input[type='checkbox']").attr('checked','checked');​​​​​​​​​​​​​​​​​​​​

Upvotes: 0

EthanB
EthanB

Reputation: 4289

Use http://api.jquery.com/checkbox-selector/

jQuery(':checkbox').click();

Upvotes: -1

Related Questions