views:

195

answers:

2

prelude: I'm sure this code is ugly so feel free to suggest a better way.

the goal: making a little web store that lets people preview their different configurations of a given product (a belt) as they make their selections. (e.g. red belt with a gold buckle vs red belt with a silver buckle, etc.)

you can see a working version here: http://solomongiles.com/demos/deadcowbelts/choose-a-belt/trial2.html

and here's my ugly jquery.

$(document).ready(function(){

 $("img").addClass("hide");
 $("img.belt-black").removeClass("hide");
 $("img.buckle-gold").removeClass("hide");
 $("img.coins-gold").removeClass("hide");


 $("input.belt-black").click(function(event){
    $("img.belt-black").removeClass("hide");
    $("img.belt-brown").addClass("hide");
    $("img.belt-red").addClass("hide");  
  });
 $("input.belt-brown").click(function(event){
    $("img.belt-black").addClass("hide");
    $("img.belt-brown").removeClass("hide");
    $("img.belt-red").addClass("hide");  
  });
 $("input.belt-red").click(function(event){
    $("img.belt-black").addClass("hide");
    $("img.belt-brown").addClass("hide");
    $("img.belt-red").removeClass("hide");  
  });

 $("input.buckle-gold").click(function(event){
    $("img.buckle-gold").removeClass("hide");
    $("img.buckle-silver").addClass("hide");
  });
 $("input.buckle-silver").click(function(event){
    $("img.buckle-gold").addClass("hide");
    $("img.buckle-silver").removeClass("hide");
  });

 $("input.coins-gold").click(function(event){
    $("img.coins-gold").removeClass("hide");
    $("img.coins-silver").addClass("hide");
  });
 $("input.coins-silver").click(function(event){
    $("img.coins-gold").addClass("hide");
    $("img.coins-silver").removeClass("hide");
  });

 });

thank you community! you're great. :)

jon

A: 

Here's a method without JQuery, that uses the cascading in CSS instead of hiding and showing each element. The class name of the belt div is set to control which images are visible.

I put the text for the radio buttons in label elements and linked them to the radio buttons, so now you can click on the text also, not only the radio button itself.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>Custom Belt Prototype</title>

<script type="text/javascript">

var belt = 'black', buckle = 'gold', coins = 'gold';

function setClass() {
   document.getElementById('belt').className = 'belt-'+belt+' buckle-'+ buckle+' coins-'+coins;
}

function setBelt(color) {
   belt = color;
   setClass();
}

function setBuckle(color) {
   buckle = color;
   setClass();
}

function setCoins(color) {
   coins = color;
   setClass();
}

</script>

<style type="text/css">

body { margin: 0; padding: 0; }

#belt { position: relative; top: 10px; margin: 0 auto; width: 652px; }
#belt-triggers { position: relative; top: 100px; margin: 0 auto; width: 110px; }

#belt img { position: absolute; display: none; }
#belt.belt-black .belt-black,
#belt.belt-brown .belt-brown,
#belt.belt-red .belt-red,
#belt.buckle-gold .buckle-gold,
#belt.buckle-silver .buckle-silver,
#belt.coins-gold .coins-gold,
#belt.coins-silver .coins-silver { display: block; }

</style>

</head>
<body>

<div id="belt" class="belt-black buckle-gold coins-gold">

<img class="belt-black" src="faux-belt/belt-black.png" alt="belt-black" width="652" height="75"/>
<img class="belt-brown" src="faux-belt/belt-brown.png" alt="belt-brown" width="652" height="75"/>
<img class="belt-red" src="faux-belt/belt-red.png" alt="belt-red" width="652" height="75"/>

<img class="buckle-gold" src="faux-belt/buckle-gold.png" alt="buckle-gold" width="652" height="75"/>
<img class="buckle-silver" src="faux-belt/buckle-silver.png" alt="buckle-silver" width="652" height="75"/>

<img class="coins-gold" src="faux-belt/coins-gold.png" alt="coins-gold" width="652" height="75"/>
<img class="coins-silver" src="faux-belt/coins-silver.png" alt="coins-silver" width="652" height="75"/>

</div>

<div id="belt-triggers">

<input class="belt-black" name="belt" id="belt_black" type="radio" checked="checked" onclick="setBelt('black');" /> <label for="belt_black">belt: black</label><br />
<input class="belt-brown" name="belt" id="belt_brown" type="radio" onclick="setBelt('brown');" /> <label for="belt_brown">belt: brown</label><br />
<input class="belt-red" name="belt" id="belt_red" type="radio" onclick="setBelt('red');" /> <label for="belt_red">belt: red</label><br /><br />

<input class="buckle-gold" name="buckle" id="buckle_gold" type="radio" checked="checked" onclick="setBuckle('gold');" /> <label for="buckle_gold">buckle: gold</label><br />
<input class="buckle-silver" name="buckle" id="buckle_silver" type="radio" onclick="setBuckle('silver');" /> <label for="buckle_silver">buckle: silver</label><br /><br />

<input class="coins-gold" name="coins" id="coins_gold" type="radio" checked="checked" onclick="setCoins('gold');" /> <label for="coins_gold">coins: gold</label><br />
<input class="coins-silver" name="coins" id="coins_silver" type="radio" onclick="setCoins('silver');" /> <label for="coins_silver">coins: silver</label>

</div>

</body>
</html>
Guffa
thanks!! you're great!
Why the downvote (whoever that was)? If you don't say what it is that you don't like, it's rather pointless...
Guffa
crap. it doesn't work in IE6.
That's odd... What specifically do you mean by "doesn't work"? What happens?
Guffa