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

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-27-2011, 06:24 PM   PM User | #1
vincentccw
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
vincentccw is an unknown quantity at this point
Switching image back and forth using OnClick?

I want to create the form button similar to this:
http://www.customusb.com/quoteform-wref-flashdrives.php

Im a designer and i m a total noob in javascript, would appreciate very much if you could be patient with me....

i use an OnClick to switch the image but the if else statement just doent let me switch back to the original image, i not sure where i have gone wrong...

appreciate again if anyone could help me out...

below are the html and js thumnails:
http://img215.imageshack.us/f/73258715.jpg/
http://img35.imageshack.us/f/44225148.jpg/
vincentccw is offline   Reply With Quote
Old 03-28-2011, 12:54 AM   PM User | #2
oVTech
Regular Coder

 
oVTech's Avatar
 
Join Date: Nov 2010
Location: USA
Posts: 296
Thanks: 4
Thanked 54 Times in 52 Posts
oVTech is an unknown quantity at this point
Quote:
Originally Posted by vincentccw View Post

Im a designer and i m a total noob in javascript, would appreciate very much if you could be patient with me....
Lucky you! I'm very patient today....
Look in the comments for instructions on how to use the script. Good luck!
Code:
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Click Rollovers</title>
<script type="text/javascript">
/* <![CDATA[ */

	//Cross-Browser Events (required)
	function addEvent(elm, evType, fn) {
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, false);
		} else if (elm.attachEvent) {
			elm.attachEvent('on' + evType, fn);
		} else {
			elm['on' + evType] = fn;
		}
	}
	
	//Generic function for retrieving an event's target (required)
	function getTarget(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){
			target=target.parentNode;
		}
		return target;
	}

rollover = {
	preLoads:[],
	
	init: function () {
		var imgs = document.images; 
		for( var i=0; i<imgs.length; i++ ) { //loop through the images
			if ( imgs[i].className == 'roll' ) { //if they have the 'roll' className then...
				var oversrc = imgs[i].src.replace('.', ('_on' + '.') ); //create a variable and store the on/rollover version of each image, which is the initial image's name plus the "_on" addon
				rollover.preLoads[i] = new Image(); 
				rollover.preLoads[i].src = oversrc; //Preload the on/rollover image
				addEvent( imgs[i], 'click', rollover.roll ); //onclick...
			}
		}
	},
	
	roll: function (e) {
		var t = getTarget(e);
		var c = t.src; 
		var ison = c.indexOf('_on'); 
		if ( ison == -1  ) {//If the clicked image does not have the "_on" addon then...
			t.src = c.replace( '.', ('_on' + '.') ); //we add the "_on" addon to that clicked image
		}
		else { //else we take it out
			t.src = c.replace( ('_on' + '.'), '.' );		
		}	
	}
};		

addEvent( window, 'load', rollover.init );

/* ]]> */
</script>
</head>
<body>
	<!-- There must be an image named "first_on" in your directory -->
	<img src="first.png" class="roll" alt="" /> <br />
	
	<!-- There must be an image named "second_on" in your directory -->
	<img src="second.png" class="roll" alt="" />
	

	<!--//Add as many as you want... ***remenber the "_on" addon//-->
</body>
</html>
__________________




I don't know, I don't care, and it doesn't make any difference!
-Albert Einstein-



oVTech is offline   Reply With Quote
Reply

Bookmarks

Tags
image, onclick, source, src, switch

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 09:24 PM.


Advertisement
Log in to turn off these ads.