HLB
HLB

Reputation: 558

Switch Statement Not Working with Click event

Im really trying to keep my code clean by using the switch statement but it seems its not working some how. I have made an array of objects that have the class .ClientButtonPic and if I write

var clientButtonNumber = $(".ClientButtonPic");     
$(clientButtonNumber[0]).click(function(){ $(".ClientImages:eq(0)").fadeIn(300); 
$(".ClientImages:eq(1)").fadeOut(300); });

It works fine but since I have 6 instances that can be clicked I wanted to use the switch statement. Heres my code:

var clientButtonNumber = $(".ClientButtonPic");
$(clientButtonNumber).click(function(){
    switch(this)
     {
case 0:
$(".ClientImages:eq(0)").fadeIn(300);
$(".ClientImages:eq(1)").fadeOut(300);
  break;
case 1:
$(".ClientImages:eq(1)").fadeIn(300);
$(".ClientImages:eq(0)").fadeOut(300);
  break;
default:
  break;
         }
     }); 

Any help is appreciated.

Upvotes: 1

Views: 894

Answers (1)

Kevin B
Kevin B

Reputation: 95047

You need to use the index of this.

switch( $(this).index(".ClientButtonPic") )

Upvotes: 1

Related Questions