Anyone good at JavaScript

Associate
Joined
17 Nov 2008
Posts
101
Hi guys,

I'm currently trying to convert this

Code:
function mouseoverhome()
{
document.home.src ="home2.png"; //changes the image source atrribute of 'home'
}


function mouseouthome()
{
document.home.src ="home1.png";
}


function mouseovercontactus()
{
document.contactus.src ="contactus2.png";
}


function mouseoutcontactus()
{
document.contactus.src ="contactus1.png";
}

etc for each button

Into something more like this


Code:
function rollover(name, over)

var over;
var overorout;
var name;
var imgsrc 
{
	switch(over)
	{
	case 1:
		overorout = "2"
		break
	case 0: 
		overorout = "1"
		break
	}
	
	imgsrc = 'document.' + name + '.src = "' + name + overorout + '.png"';
}

I didn't think this through much before I got into it, but hopefully you can see what I'm trying to do.

Instead of each button having 2 of its own functions for rolled over/rolled off, I'm trying to use the same function with every button using parameters.

I have been able to get the full command that I need to change the image displayed on a button when a user rolls over, but it's stored in a variable as a string. Is there anyway I can use this?

If this makes no sense whatsoever let me know and I'll try to make it more clear.
 
Thank you for your help Tripnologist.

This JavaScript stuff I'm doing is for college and the bit im working on right now actually has nothing to do with my assignment at all it's just me being curious. I'm going to get round to learning about jQuery soon, I feel so bad about learning random things that currently I don't have any lessons on when I have **** like HCI assignments to be doing
:(.

That CSS Sprite thing looks very interesting and I'm definitely going to revisit it.

To anyone who thinks they have the same problem as me, check out Crowze's post and remember if you are using 2 sets of quotes on the same line, alternate "s and 's.

Code doing what I want it to
Code:
function rollover(name, over)
{

	imgsrc = 'document.' + name + '.src = "' + name + over + '.png"';
	eval(imgsrc);
}
 
Last edited:
Nooooo don't use eval, it's pure evil. You can easily do it by using the document like an array, as follows:
Code:
function rollover(name, over)
{
	document[name].src = name + over + '.png';
}

Ok then I will, just for my curiosity could you tell me why eval is evil :P
 
Back
Top Bottom