Thursday, March 28, 2013

Manipulate Checkboxes On Any*** Page

Copy and paste the following line into your browser address bar: 

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



*** Will not work for older versions of (Internet Explorer) IE
(1) document.querySelectorAll not available untile IE v8
(2) forEach not available until IE v9 

The use of a JavaScript toolkit/framework, e.g., JQuery, relieves the developer from having to worry about incompatibility issues like the ones mentioned above.


Verify that your browser didn't automatically remove the "javascript:" from its address bar, assuming you copy/pasted the javascript.  If so, then just hand-type javascript: at the beginning and press your enter key.

Test it out here:



Alice
Bob
Cindy



JQuery Set all Checkboxes Version


If the page already has JQuery loaded, then you have less to type...

javascript:$(":checkbox").prop("checked", true);


You probably noticed that this didn't work on this blog page.

That's because this page is not using (and has not loaded) the JQuery library.

So, to dynamically load JQuery on a page, e.g., this blog page, paste the following in the address bar:



javascript:(function () {
  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
  document.getElementsByTagName('body')[0].appendChild(s);
})();$(":checkbox").prop("checked", true);




Sponsor Ads



-->
-->

4 comments:

  1. readings were really very good. I will bookmark this blog so that I could often visit this blog often to read the latest article from your blog. I wait for your arrival at our website ...

    thanks, ...


    By : ios development jakarta | firzil.co.id

    ReplyDelete


  2. I also agree with you ..... very useful information for us ...... keep it up thanks for this..........






    Application Development

    ReplyDelete
  3. how to select only one or 2 of the 3 above checklist using this trick?

    ReplyDelete
  4. Vinay,

    To check the 2nd and 3rd checkboxes, I'd do something like this:

    $(":checkbox").filter(function(i) {
    return $.inArray(i, [1, 2]) > -1;
    }).prop("checked", true);

    Thank you for the question,

    ReplyDelete