By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

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

P: 34
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
Share this Question
Share on Google+
3 Replies


P: 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
Expert Mod 5K+
P: 5,343
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
Expert Mod 15k+
P: 16,027
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.