views:

57

answers:

1

I am trying to figure out why this code doesn't work..

All i want is to have simple event delegation to assign one event listener.

it only alerts, it doesn't animate. Please let me know whats wrong here:

$(document).ready(function() {
  var img = $("img");

$("span").click(function(e){
  var targetClicked = $(e.target).attr('class');
  //the alert works fine
  alert(targetClicked)

switch(targetClicked){
// i deleted the rest of the cases
    case d:img.stop(false,true);
  break;
    case e:img.slideDown().animate({"width":200, height:200, opacity:0.4,});
  break; 
  //nothings works here as well
    case f:alert("hi");
  break;
 }
 });      
});
+3  A: 

What are d and e in your switch statement case conditions? The way you're code is written right now, they're being treated as variables and your code is probably blowing up with a "'d' is undefined" error.

If you want to switch on the class names "d" and "e", then you need to use the class names as strings:

switch (targetClicked) {
  case "d":
    //...
    break;
  case "e":
    // ...
    break;
}
Annabelle
yeps, this is it. got it, thanks.
adardesign
+1 Danget...I was typing a similar response and got a phone call from my credit card company =P
Topher Fangio
Haha, I can't count the number of times I've seen that dreaded "you lost the typing race" banner :P
Annabelle