/    Sign up×
Community /Pin to ProfileBookmark

Image Swapping and Function

Hello,

I am having a small problem with functions in Javascript.

My task is to create 3 buttons, when one gets clicked it is switched on or off.
Rather than have SUBs for each button, I wanted to use a function.

This is the function:

function flip(FlipToImage) {

if (OnOff==’Off’)
{
document.images[FlipToImage].src = ‘Graphics/Tick.png’
OnOff=’On’;
}
else
{
document.images[FlipToImage].src = ‘Graphics/EmptyButton.png’
OnOff=’Off’;
}
}

And this is the button-code:

<a href=’#’ onClick=’flip(“Image1”);return false’><img src=’Graphics/EmptyButton.png’ name=’image1′></a>

<a href=’#’ onClick=’flip(“Image2”);return false’><img src=’Graphics/EmptyButton.png’ name=’image2′></a>

<a href=’#’ onClick=’flip(“Image3”);return false’><img src=’Graphics/EmptyButton.png’ name=’image3′></a>

So – when you click on Image1, the flip kicks in and is supposed to switch the corresponding button on or off. etc.. for image2 and image3.

The error seems to be related to the part where
document.images[FlipToImage].src

I’ve tried putting document.images[‘FlipToImage’].src but that makes no difference.

Can you not you a function variable in an object?

Thanks!

Carl.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMay 07.2004 — Javascript is CaSe SeNsItIvE. Check your capitalization, since "Image1" is not the same as "image1".
Copy linkTweet thisAlerts:
@garpaauthorMay 07.2004 — Well. Doh!


Too used to VB/ASP !!

Thank you very much! Worked a treat...


Now all I have to do is create a dynamic table and when one button is switched on in a row, the other 2 have to be switched off.... ?

Thanks AGAIN!


Carl.
×

Success!

Help @garpa 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.15,
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,
)...