Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Selects none and select all checkboxes

Posted on 2013-01-25
2
Medium Priority
?
288 Views
Last Modified: 2013-01-25
Hi all,

I have some checkboxes on a page that by default I have checked when the page loads. I would like to add a "Select None" and "Select All" buttons to the bottom of the pages so the user can quickly deselect and select all the options:

Here's the format I currently have them in :


<li class="list">
      <img src="images/hak.jpg" height="100" width="120" />
      <label class="filter">Hak
      <input type="checkbox" name="checkbox1" value="2" checked/>
      </label>
 	  </li>  
      <li class="list">
      <img src="images/ges.jpg" height="100" width="120" style="" />
      <label class="filter">Ges
      <input type="checkbox" name="checkbox2" value="4" checked/>
      </label>
      </li>

Open in new window

0
Comment
Question by:TLN_CANADA
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 38821123
Test page : http://jsfiddle.net/5dxra/
using :
<input type="checkbox" checked="checked" id="checky" /><label for="checky" id="forchecky">Uncheck all</label>

Open in new window

and :
window.onload = function() {
    document.getElementById("checky").onchange = function() {
        setAllCheckboxes(this.checked);
        document.getElementById("forchecky").innerHTML = this.checked?"Uncheck all":"Check all";
    }
}
function setAllCheckboxes(state) {
    var inputs = document.getElementsByTagName("input");
    for(var i=0;i<inputs.length;i++)  if(inputs[i].name && /checkbox\d/.test(inputs[i].name)) inputs[i].checked = state;
}

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38821144
Works perfectly! Thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question