0

I need something like a if else function in my jquery but I don't know how I have to do that. Or isn't it possible?

 if  $(".image1").click(function(){

        $(idnumbers).css("display", "block");
        $(idnumberp).css("display", "none");
        $(idnumber).css("display","block");  
  });



 else if $(".image2").click(function(){
        $(idnumbers2).css("display", "block");
        $(idnumberp).css("display", "none");
        $(idnumber).css("display","block");  

  });



 else $(".image3").click(function(){

        $(idnumbers3).css("display", "block");
        $(idnumberp).css("display", "none");
        $(idnumber).css("display","block");  

   });
6
  • 9
    What are you trying to do? Do you not just want the 3 event handlers? Commented May 18, 2012 at 14:13
  • 2
    Those are event handlers; you don't need the if or else at all. They will only run when the element is clicked. Commented May 18, 2012 at 14:13
  • The .click function doesn't fire until someone clicks on that element so you shouldn't need the if else block. Commented May 18, 2012 at 14:14
  • 2
    Please avoid linking to w3schools.com. Commented May 18, 2012 at 14:16
  • 2
    @dezso: w3schools is a wrong and misleading site. You shouldn't use it as reference for any sort of language. For PHP, there's the PHP Manual, for JavaScript, there's Mozilla Developer Network (or MDN). See w3fools.com to further understand why you should never use w3schools. Commented May 18, 2012 at 14:16

2 Answers 2

2

You don't need to put your event handlers in to if statements. By attaching the event to the element 'onClick' listener, you're already handling each specific case.

$(".image1").click(function(){
    $(idnumbers).css("display", "block");
    $(idnumberp).css("display", "none");
    $(idnumber).css("display","block");  
});

$(".image2").click(function(){
    $(idnumbers2).css("display", "block");
    $(idnumberp).css("display", "none");
    $(idnumber).css("display","block");  
});

$(".image3").click(function(){
    $(idnumbers3).css("display", "block");
    $(idnumberp).css("display", "none");
    $(idnumber).css("display","block");  
});

This should do what you need without the extraneous if/else statements.

Sign up to request clarification or add additional context in comments.

Comments

2

You really want something like this:

function imageClick()
{       
    $(idnumberp).css("display", "none");         
    $(idnumber).css("display","block");     
};

$(".image1").click(function(){ $(idnumbers).css("display", "block"); imageClick(); });
$(".image2").click(function(){ $(idnumbers2).css("display", "block"); imageClick(); });
$(".image3").click(function(){ $(idnumbers3).css("display", "block"); imageClick(); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.