471,887 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

how to check/uncheck a number of checkboxes by clicking an image


I want to check or uncheck a number of checkboxes by clicking an image.

My html code is given below:

<div id = "checkdiv" >
<img src = "/images/check.jpg" />
<input type = "checkbox" name = "del">
<input type = "checkbox" name = "del">


Now if i click image or div all the checkboxes should be checked and again if i click that image or div all the checkboxes should be unchecked.
Jul 24 '08 #1
3 3180
i think you must make another event.
For example, onclick for check the checkbox and another event for uncheck the chekbox..

But hope this can usefull...

first, change your "a" tag with this

[HTML]<a href="#" onclick="javascript:setBox();" ><img src="/images/check.jpg"></a>[/HTML]

second. Better use a unique name for checkbox.
The first checkbox may named "del1", and second checbox goes to "del2".
Of course this is depends with your need

than after the body, make the script

[HTML]<script type="text/javascript">
function setBox() {
var form = document.forms[0];

and as i said, to uncheck the checbox, i think you need another event,,


kind regards,
Jul 24 '08 #2
5,390 Expert Mod 4TB
the javascript: part in the following line is never needed:

Expand|Select|Wrap|Line Numbers
  1. onclick="javascript:setBox();"
the following:

Expand|Select|Wrap|Line Numbers
  1. onclick="setBox();"
is always enough ...

kind regards
Jul 24 '08 #3
16,027 Expert Mod 8TB
Make one function which checks/unchecks all checkboxes and pass it a parameter to determine whether it should be checked or not, e.g.
Expand|Select|Wrap|Line Numbers
  1. function checkUncheckBoxes(check) {
  2.   var chk = document.getElementsByName(chckboxname);
  3.   for (i = 0; i< chk.length; i++) {
  4.     chk[i].checked = check;
  5.   }
  6. }
Jul 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by pw | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.