Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2011, 06:14 PM   PM User | #1
jimbojones
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jimbojones is an unknown quantity at this point
Alternate images and div content

Hi,

I am currently working on a project that requires javascript onClick events.

What I am looking to achieve is that when an image is clicked, the content is shown and the image changes to another image, and when this is clicked again the image reverts back and the content becomes hidden

Currently I have this in script tags

Code:
<script language="javascript" type="text/javascript">

function setVis(id, vis) {
document.getElementById(id).style.display = vis;
}

function swapImage() {
switch (imageNo) {
 case 1:
   image.src = "images/img.png"
   imageNo = 2
   return(false);
case 2:
   image.src = "images/img1.png"
   imageNo = 1
   return(false);
 }
}

</script>
and this in the main html page

Code:
<h4><a href="#" onclick="setVis('content','inline');return false;"><img id="image" name="image" src="images/img.png" width="10" height="10" border="0" onclick="swapImage();"/></a> Content</h4>
This doesn't work it shows the content but it won't hide it again, and the images do not change.

I would appreciate any help that can be offered thanks

Last edited by jimbojones; 03-03-2011 at 06:19 PM..
jimbojones is offline   Reply With Quote
Old 03-04-2011, 11:33 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Code:
switch (imageNo) {
What supposes to be the variable imageNo? You have not defined it before the switch.

Could be something like this:
Code:
function swapImage(img) {
img.src=img.src.match(/img1/)?'images/img.png':'images/img1.png';
}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 03-04-2011 at 11:37 AM..
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 PM.


Advertisement
Log in to turn off these ads.