469,323 Members | 1,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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

Hi,

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

My html code is given below:
[HTML]

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

[/HTML]

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 3081
maminx
77
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];
form.del1.checked=true;
form.del2.checked=true;
}
</script>[/HTML]

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

thanks..

kind regards,
maminx
Jul 24 '08 #2
gits
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
acoder
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.