28

I am having 100 Checkboxes on my web page. For testing purposes I want to tick all those boxes, but manually clicking is time consuming. Is there a possible way to get them ticked?

Perhaps a JavaScript or Chrome Console window, anything?

4
  • A bookmarklet in pure JS maybe? Commented Jun 6, 2012 at 4:35
  • @FabrícioMatté - I am unaware of what it is ? Could you please explain ? Commented Jun 6, 2012 at 4:38
  • 7 answers.. as I expected, there's enough explanations here :P Anyway, I submitted my own answer with detailed explanation/instructions. Commented Jun 6, 2012 at 4:52
  • Most probably it is for the field level permissions in salesforce. Commented Nov 18, 2019 at 11:47

10 Answers 10

64

The most direct way would be to grab all your inputs, filter just the checkboxes out, and set the checked property.

var allInputs = document.getElementsByTagName("input");
for (var i = 0, max = allInputs.length; i < max; i++){
    if (allInputs[i].type === 'checkbox')
        allInputs[i].checked = true;
}

If you happen to be using jQuery—and I'm not saying you should start just to tick all your checkboxes for testing—you could simply do

$("input[type='checkbox']").prop("checked", true);

or as Fabricio points out:

$(":checkbox").prop("checked", true);
Sign up to request clarification or add additional context in comments.

5 Comments

$(':checkbox') is a handy shorthand for $("[type='checkbox']") as well.
@Derek- you're right - that's why I gave the pure JS answer first :)
Use crunchinator to convert the code above to a bookmarklet (unfortunately the 'let doesn't work with frames, fortunately Firefox can "Show only this frame"): Bookmarklet Crunchinator
You can use javascript: <code> and jam it into the address bar and hit return and it works perfectly!
It works, I used to uncheck 1000 checkboxes at once, changing to false allInputs[i].checked = false; Thanksss!
18

Pure JS method, don't use jQuery.. its just silly for something so trivial.

[].forEach.call( document.querySelectorAll('input[type="checkbox"]'),function(el){
       el.checked=true;
     }
);​

Live Demo

To use it on any webpage you can paste this into the address bar

javascript:[].forEach.call(document.querySelectorAll('input[type="checkbox"]'),function(el){el.checked=true});

then drag that to your bookmarks, and you have a bookmarklet. Just click it whenever you need to use it on a page.

5 Comments

+1: Notes: Used functions: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…, developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…. Compatibility notice: forEach isn't implemented until version 9 in IE, document.querySelectorAll was implemented in version 8.
@Zeta yeah wont work in IE<8, but I chose forEach because he mentioned the chrome console, so I assume hes using a modern browser.
@Loktar: strange to read 'don't use jQuery.. its just silly for something so trivial.' isn't it?
@Talha not sure what you mean? But jQuery is crazy overkill for something like this. No need to include 2000 lines of code to check some check boxes.
@Loktar How can I check only one specific checkbox from a selection of many? The code line looks like <input class="type" type="checkbox" data-bind="checked: checked, value: id" value="1"> (there are many more checkboxes with higher values from 2 to 13.)
7

querySelectorAll is your best choice here if you don't want jQuery!

var ele = document.querySelectorAll("input[type=checkbox]");
for(var i=0;i<ele.length;i++){
    ele[i].checked = true;
}
//Done.

Comments

5

by using jquery, simple as that

$('input:checkbox').each(function () {
   // alert(this);
   $(this).attr('checked', true);
  });

Or simply use

$('input:checkbox').prop('checked', true);// use the property

OR

 $('input:checkbox').attr('checked', true); // by using the attribute

1 Comment

I find using Chrome console, .prop('checked', true) visibly turns on the checkbox. .attr('checked', true) does not. I don't understand, as .attr is much more often suggested but it doesn't work.
5

Just paste one of these one-liners to your browser console:

Tick all checkboxes:

document.querySelectorAll('input[type="checkbox"]').forEach(e => e.checked = true);

Untick all checkboxes:

document.querySelectorAll('input[type="checkbox"]').forEach(e => e.checked = false);

Comments

2

This JS code will check all checkboxed in your page:

var a = document.querySelectorAll('input[type="checkbox"]');
for (var i=0; i<a.length; i++)
    a[i].checked = true;​

Live demo

All you have to do then is create a bookmarklet with it, say, with this bookmarklet maker, which generates this bookmarklet code:

javascript:var a=document.querySelectorAll('input[type="checkbox"]');for(var i=0;i<a.length;i++)a[i].checked=true;%E2%80%8B

Just add this URI to a bookmark in your bookmark toolbar, then all you have to do is click it whenever you need all the checkboxes in your page to be checked. =]

Comments

2

Multiple Check All & Uncheck All Boxes

All You Need to change is the tag 'name' to change the what its turing ON/OFF

<FORM>
    <input type="checkbox" name="item0[]" onclick="CheckAll(this)" />
    <input type="checkbox" name="item0[]" value="1" />
    <input type="checkbox" name="item0[]" value="2" />
    <input type="checkbox" name="item0[]" value="3" />
    <br>
    <input type="checkbox" name="item1[]" onclick="CheckAll(this)" />
    <input type="checkbox" name="item1[]" value="a" />
    <input type="checkbox" name="item1[]" value="b" />
    <input type="checkbox" name="item1[]" value="c" />
    <br>
    <input type="checkbox" name="item2" onclick="CheckAll(this)" />
    <input type="checkbox" name="item2" value="a1" />
    <input type="checkbox" name="item2" value="b2" />
    <input type="checkbox" name="item2" value="bc" />
</FORM>

<script>
    function CheckAll(x)
    {
        var allInputs = document.getElementsByName(x.name);
        for (var i = 0, max = allInputs.length; i < max; i++) 
        {
            if (allInputs[i].type == 'checkbox')
            if (x.checked == true)
                allInputs[i].checked = true;
            else
                allInputs[i].checked = false;
        }
    }
</script>

Comments

2

I provided answer to this question at Check all Checkboxes in Page via Developer Tools

In short you can do it from dev tools console (F12) by:

$$('input').map(i => i.checked = true)

or

$$('input[type="checkbox"').map(i => i.checked = true)

1 Comment

The best answer, but I had to use the click method to make this work: $$('input[type="checkbox"]').forEach(i => { if (!i.checked) i.click() } )
0

The following code will toggle all checkboxes. I think this is useful in case you want that feature. If you check a box it will uncheck that box. I know this doesn't answer the question technically but I wanted to put it up here because it's what I use. Thanks for the comment. I hope this answer is better suited to your pallette.

//Select All Checkboxes On Page
allSelects = document.querySelectorAll('input[type="checkbox"]');
for(i=0;i<allSelects.length;i++){
  allSelects[i].click();
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1
  function selectAll(elem)
  {
  for (i = 0; i < elem.length; i++)
    elem[i].checked = true ;
  }

On Click of a button call this method and pass the name of the element(checkboxes-they all should be same named).

2 Comments

We want this to be done using console tab of any browser not using any button call.
@DadhichSourav, As per the stated question there is "or" in your query. Which means any way to do it (Anything).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.