[SalesForce] Apex Command Button onclick condition

This is a generic question

Can I add <apex:commandbutton onclick="if condition? result 1 else result 2">

I would really appreciate a simple example if this is possible.

Thanks in advance

Best Answer

Check like this for If part use below code

<apex:commandbutton onclick="if(true){
                                  alert('this is if part');
                              }else{
                                 alert('this is else part'); 
                               }">

and for else part

<apex:commandbutton onclick="if(false){
                                      alert('this is if part');
                                  }else{
                                     alert('this is else part'); 
                                   }">

Edit --

Goto w3school code editor http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_test

and run below code

<!DOCTYPE html>
<html>
<body>

<button type="button" onclick="if(true){
                                  alert('this is if part');
                              }else{
                                 alert('this is else part'); 
                               }">Click If!</button>

<button type="button" onclick="if(false){
                                  alert('this is if part');
                              }else{
                                 alert('this is else part'); 
                               }">Click Else!</button>
</body>
</html>