Forum Moderators: open

Message Too Old, No Replies

make image check checkbox

         

nameht

9:07 am on Jul 7, 2005 (gmt 0)

10+ Year Member



How can I make it so when i click an image it checks a checkbox directed underneath it?

What I have is a list of product names/image/checkbox

and instead of trying to click the checkbox I want to be able to click the image and the text

is that possible?

j4mes

10:16 am on Jul 7, 2005 (gmt 0)

10+ Year Member



Hi nameht, welcome to WebmasterWorld!

This should do the trick:


<html>
<head>
<script type="text/javascript">
function check() {
document.getElementById("checkbox").checked = "checked";
}
</script>
</head>
<body>
<img src="YOUR_IMAGE" onclick="javascript:check();">
<input type="checkbox" id="checkbox" />
</body>
</html>

If you want text also, place both image and text into a div and give the onclick handler to the div.

HTH, J.

nameht

3:54 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



Hi thanks jam4s that works great when i have one image,
but on this page I plan to have 100 images all with checkboxes (and unique id)

so is there a way to pass a variable to have maybe
<img src="YOUR_IMAGE" onclick="javascript:check(123);">
where 123 would be the id # for that picture?

j4mes

6:02 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



But of course :-)

<html>
<head>
<script type="text/javascript">
function check(checkboxid) {
document.getElementById(checkboxid).checked = "checked";
}
</script>
</head>
<body>
<img src="YOUR_IMAGE" onclick="javascript:check('checkbox');">
<input type="checkbox" id="checkbox" />
</body>
</html>

J.