scriptygoddess

06 Mar, 2003

Toggle an image's display based on a checkbox

Posted by: Jennifer In: Scripts

The idea here is to display an image based on whether a check box is checked or not. It'll toggle it's display, so you can click it on, click it off, etc.

(And if you post a comment asking me why anyone would ever want this, I will beat you up. LOL! I needed this for an application I was working on at work. I'm sure there are other uses as well… can't think of them at the moment, but that's not my problem.) 😉

This goes in between your <head> </head> tags:

<script language="javascript">
<!–
function toggleImage(imgName, imgOne, blankImg) {
if (document[imgName].src == imgOne) {
document[imgName].src = blankImg;
} else if (document[imgName].src == blankImg) {
document[imgName].src = imgOne;
}
}
//alternatively, you can use this function
//if you're sure you're going to be toggling between the same images
function toggleImage2(imgName) {
if (document[imgName].src == "HTTP://YOURDOMAIN.COM/PATH/TO/IMAGE1.gif") {
document[imgName].src = "HTTP://YOURDOMAIN.COM/PATH/TO/spacer.gif";
} else if (document[imgName].src == "HTTP://YOURDOMAIN.COM/PATH/TO/spacer.gif") {
document[imgName].src = "HTTP://YOURDOMAIN.COM/PATH/TO/IMAGE1.gif";
}
}
//–>
</script>

Now on your page, your checkbox looks like:

<input type="checkbox" name="displayImage" value="displayImage" onClick="toggleImage('thePictureName', 'HTTP://YOURDOMAIN.COM/PATH/TO/IMAGE1.gif', 'HTTP://YOURDOMAIN.COM/PATH/TO/spacer.gif')">

or this (depending on which function you're using:

<input type="checkbox" name="displayImage" value="displayImage" onClick="toggleImage2('thePictureName')">

Then your image is simply set to the default:

<img src="images/spacer.gif" name="thePictureName" width="10" height="10">

7 Responses to "Toggle an image's display based on a checkbox"

1 | joelle

March 6th, 2003 at 12:08 pm

Avatar

I can't believe you just posted this. i spent 99% of yesterday working on this very thing, but in Flash.

Kudos! 😉

2 | dave

March 6th, 2003 at 8:10 pm

Avatar

I can think of a use for it, actually. We have a stylesheet switcher on our site. One of the styles is a "stealth" style. It's made to look like what Ghostzilla does to a page, so people can view the site inconspicuosly at work. If we decide to use a logo graphic, this would give people wanting to surf clandestinely the ability to turn the logo graphic off. It would be especially cool if it had a cookie feature, so it would always remember not to display the graphic for a given user.

3 | grayone

May 21st, 2003 at 11:26 pm

Avatar

Good script, and I can think of ways to use it. I'll try it on my 'test' site… sorry for the dang popup ads!

4 | Richard

July 28th, 2003 at 5:08 pm

Avatar

I can't tell what I'm missing for the toggle 2 script. I don't get an error, but the link doesn't change.

<script language="javascript">
<!–

function toggleImage2(imgName) {
if (document[imgName].src == "images/linkOn.gif")
{
document[imgName].src = "images/linkOff.gif";
}
else if (document[imgName].src == "images/linkOff.gif")
{
document[imgName].src = "images/linkOn.gif";
}
}

//–>
</script>
</head>

<body>
<input type="checkbox" name="displayImage" value="displayImage"
onClick="toggleImage2('thePictureName')">

<img src="images/linkOff.gif" name="thePictureName">
</body>

5 | chung

January 23rd, 2004 at 11:06 am

Avatar

Hi Guys,

Just wondering if anyone got this working? I am getting the javascript error

"document[..]src' is null or not an object?"

I believe it may be a wrong image name or something, but I've checked and this doesn;t seem to be the case!

Arggh

Thanks

6 | Sanjeev Jha

March 2nd, 2004 at 12:06 pm

Avatar

Hi,
I am able to toggle the image but it gets reset to the first image all over again.

Please let me know, what ou think the reson for it doing so.

7 | WE ARE HUGH

March 6th, 2003 at 1:28 pm

Avatar

Toggle an image's display based on a checkbox
scriptygoddess: "It'll toggle it's display, so you can click it on,
click it off, etc."

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements