/    Sign up×
Community /Pin to ProfileBookmark

Script Help : Convert from img name -> img id

How do I convert this script from using the img name to using the img id so I can use img rollovers.

[code=php]
<script type=”text/javascript”>
{

var homeOff = new Image();
homeOff.src = “images/menu/home_off.gif”;
var homeOn = new Image();
homeOn.src = “images/menu/home_on.gif”;

var aboutOff = new Image();
aboutOff.src = “images/menu/about_off.gif”;
var aboutOn = new Image();
aboutOn.src = “images/menu/about_on.gif”;

var servicesOff = new Image();
servicesOff.src = “images/menu/services_off.gif”;
var servicesOn = new Image();
servicesOn.src = “images/menu/services_on.gif”;

var portfolioOff = new Image();
portfolioOff.src = “images/menu/portfolio_off.gif”;
var portfolioOn = new Image();
portfolioOn.src = “images/menu/portfolio_on.gif”;

var contactOff = new Image();
contactOff.src = “images/menu/contact_off.gif”;
var contactOn = new Image();
contactOn.src = “images/menu/contact_on.gif”;

var step_oneOff = new Image();
step_oneOff.src = “images/menu/step_one_off.gif”;
var step_oneOn = new Image();
step_oneOn.src = “images/menu/step_one_on.gif”
}

function inact(imgName)
{
if(document.images)
{
document[imgName].src = eval(imgName+’Off.src’);
}
}

function act(imgName)
{
if(document.images)
{
document[imgName].src = eval(imgName+’On.src’);
}
}

</script>
[/code]

And here’s the html img call for the rollovers:

[code=php]
<a href=”index.php” onmouseover=”act(‘home’);” onmouseout=”inact(‘home’);”><img src=’images/menu/home_off.gif’ alt=”Welcome to Bueza.com” style=’border: 0px;’ id=’home’ /></a>
[/code]

If someone could recode my function so that it requires the img ID isntead of the img name, that would be most appreciated!

Thanks!
Jaime Bueza

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@russellJul 16.2004 — change this

document[imgName].src = eval(imgName+'Off.src');

to

document.getElementById(imgName).src = eval(imgName+'Off.src');
Copy linkTweet thisAlerts:
@BuezaWebDevauthorJul 16.2004 — Thanks mate, this is the final script that I got help with on htmlcenter.com as well as here.

This may be of help to other developers who are doing rollovers that are valid for xhtml 1.1!

[code=php]
<script type="text/javascript">
{

var homeOff = new Image();
homeOff.src = "images/menu/home_off.gif";
var homeOn = new Image();
homeOn.src = "images/menu/home_on.gif";

var aboutOff = new Image();
aboutOff.src = "images/menu/about_off.gif";
var aboutOn = new Image();
aboutOn.src = "images/menu/about_on.gif";

var servicesOff = new Image();
servicesOff.src = "images/menu/services_off.gif";
var servicesOn = new Image();
servicesOn.src = "images/menu/services_on.gif";

var portfolioOff = new Image();
portfolioOff.src = "images/menu/portfolio_off.gif";
var portfolioOn = new Image();
portfolioOn.src = "images/menu/portfolio_on.gif";

var contactOff = new Image();
contactOff.src = "images/menu/contact_off.gif";
var contactOn = new Image();
contactOn.src = "images/menu/contact_on.gif";

var step_oneOff = new Image();
step_oneOff.src = "images/menu/step_one_off.gif";
var step_oneOn = new Image();
step_oneOn.src = "images/menu/step_one_on.gif"
}

function inact(element)
{
imgID = document.getElementById(element);
if(document.images)
{
imgID.src = eval(element+'Off.src');
}
}

function act(element)
{
imgID = document.getElementById(element);
if(document.images)
{
imgID.src = eval(element+'On.src');
}
}

</script>
[/code]


call the img now;
[code=php]
<a href="index.php" onmouseover="act('home');" onmouseout="inact('home');"><img src='images/menu/home_off.gif' alt="Welcome to Bueza.com" style='border: 0px;' id='home' /></a>
[/code]


Good luck developers! and thanks Russel for your contribution!
×

Success!

Help @BuezaWebDev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...