/    Sign up×
Community /Pin to ProfileBookmark

If statements & image swaps?

Hello,

I have a good knowledge of xhtml and css, however I’m trying to accomplish something using javascript and I’m still a beginner to it.

I’m looking to establish a t-shirt sales site. I need to be able to have a user click one of three shirts and have it appear in a main window dynamically. I can do this using simple image swap behavior found in Dreamweaver.

It gets a bit more complex and that’s where I need help:

Each shirt has a front & back for a default and alternative color. So, for one shirt, there are 4 pictures (back&front, alternative color back&front)

Once a shirt appears in the main window I need to be able to click buttons to see the front/back for that shirt. Also, I need a way to switch a given shirt in the main window between default and alternative colors, and be able to see the front/back of those colors.

I’d appreciate any guidance you have to offer.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@web_bertFeb 17.2009 — Here is my stab at it, i haven't run it though, so there could be errors

[CODE]

<img id="tshirt" src="url/to/tshirt.jpg" />

<input id="tshirt_1_image_1" type="button" value="T Shirt 1 - Image 1" />

<input id="tshirt_1_image_2" type="button" value="T Shirt 1 - Image 2" />

<script type="text/javascript">

function changeImage( idOfImageElement, urlToNewImage ) {

var img = document.getElementById( idOfImageElement ); //get the element from the DOM
if( !img ) return;//if the image is not found, exit from the function - so we don't cause an error
img.src = urlToNewImage; //assign the new url to the src attribute of the image

}

//declare the imageId, the button we want to add the click event to, the url that the src of the image with imageId should change to
var idAndSrc = [
'tshirt', 'tshirt_1_image_1', 'url/to/image_1.jpg',
'tshirt', 'tshirt_1_image_2', 'url/to/image_2.jpg'
];

//loop through the array above and add event handlers to each of the buttons
for(var i = 0, j = idAndSrc.length; i<j; i++) {
var currentInput = document.getElementById( idAndSrc[0] );

//have to use a self executing function when assigning the function, so that we can trap the current value of i
currentInput.onclick = (function( imgId, urlToChangeTo ) {

return function() {
changeImage( imgId, urlToChangeTo );
}

})( idAndSrc[0], idAndSrc[2] );
}

</script>

[/CODE]
×

Success!

Help @nockemout 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.16,
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,
)...