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

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 02-26-2011, 08:47 AM   PM User | #1
skcin7
Regular Coder

 
Join Date: Jul 2009
Posts: 186
Thanks: 72
Thanked 2 Times in 2 Posts
skcin7 is an unknown quantity at this point
jQuery function to change the source of an image???

Hello all. I am trying to make a function in jQuery that will change the SOURCE of an image. This is what I have so far but it does not work . How can this code be altered to change the source of an image???

Code:
function changeImage(imageName,imageSrc){
	//This works, but I want to try it in jQuery style function
	//document.getElementById(imageName).src = imageSrc;
	
	//This does not work, there is something wrong with imageName
	$("#imageName").attr("src", imageSrc);
}

The way I currently have my images is like this:
Code:
<a href="#" onmouseover="changeImage('home','./images/home_over.png');" onmouseout="changeImage('home','./images/home_under.png');">
<img id="home" name="home" src="./images/home_under.png" alt="HOME &amp; UPDATES" />
</a>
__________________
My website: http://www.nicholas-morgan.com/

Last edited by skcin7; 02-26-2011 at 10:11 AM..
skcin7 is offline   Reply With Quote
Old 02-26-2011, 02:19 PM   PM User | #2
harbingerOTV
Senior Coder

 
Join Date: Jan 2005
Location: Memphis, TN
Posts: 1,765
Thanks: 8
Thanked 123 Times in 121 Posts
harbingerOTV will become famous soon enough
Might not be the most graceful solution but a solution none the less:

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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Scroll :: easy version ;)</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function imgSwap(){
	$('#nav a').hover(function(){
		oldSRC = $(this).children('img').attr('src');
		newSRC = $(this).attr('rel');
		$(this).children('img').attr('src', newSRC);
	}, function(){
		$(this).children('img').attr('src', oldSRC);
	});
}



$(document).ready(function(){
	imgSwap();
});
</script>

<style type="text/css">
}

</style>

</head>
<body>

<div id="nav">
<a href="#" rel="http://farm6.static.flickr.com/5219/5478414806_41fe7057ee_t.jpg">
	<img src="http://farm6.static.flickr.com/5096/5475741180_6c392a2c21_t.jpg" alt="" />
</a>
<a href="#" rel="http://farm6.static.flickr.com/5296/5478454563_e8262f8da2_t.jpg">
	<img src="http://farm6.static.flickr.com/5058/5479010072_399d18ebe1_t.jpg" alt="" />
</a>
</div>

</body>
</html>
__________________
Stop making things so hard on yourself.
i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis
harbingerOTV 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 03:09 PM.


Advertisement
Log in to turn off these ads.