function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shiv ShankarShiv Shankar 

what is the use of return statement on onlclick event

Hi Friends,

 

What is the use of return statement here

 

<apex:commandButton action="controllerMethod()" onclick="return javaScriptMethod()" />

 

Avidev9Avidev9
adding a return does something similar to "return;" in java. But there is a lil change if the javaScriptMethod() returns true here then only associated action like invocation of "controllerMethod" will happen. else it will stop at this point.
SUMMARY :
So if you add a onclick="return false;" the command button will never execute the controller method
souvik9086souvik9086

Hi,

 

The use of return is that if the javascript method returns true value then it will be executed and do the operation, else if it retiurns false value then it will do nothing and keep the same page.

 

For e.g while clicking 'OK' and 'CANCEL' we are using return. When we click Cancel then javascript method will return false and keep in the same page and don't call the controller method.

 

If we don't use return then it will return true by default.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Shiv ShankarShiv Shankar

Ok, just see following scinario...

 

I have this command link in my vf page

<apex:commandLink action="{!addToBasket}" onclick=" return validateItemNo()" reRender="renderComponent1,renderComponent,panelId,errorPanel,panelId1" >Add to Basket</apex:commandLink>

 

I have this java script

function validateItemNo(){
            if(totalSelectItem == 0){
                alert('Please select item to add in to the basket');
                return false;
            } 
            else {
                alert(1);
                return true;
            }
            
}

 - What is happening

1. when i click on add to basket link without selecting any item it shows error. which is right. and i am returning the false because i don't want to execute action metod in this case.

2. when i click on add to basket link after selecting item it doesn't show any error, alert(1) also coming in to the picture, i am returning ture also to execute theaction method... but it doesn't call action method.

3. when i remove reRender attribute from command link it calls action method but it refreses the whole page, which i don't want.

 

please help it's urgent..

Avidev9Avidev9
What happens if you just return true from validate method ?
I mean hardcode just return true. Does that calls the controller method ?
Avidev9Avidev9

Since it is urgent.

 

I would suggest a workaround.

 

<apex:actionFunction name="exeMe" action="{!addToBasket}"  reRender="renderComponent1,renderComponent,panelId,errorPanel,panelId1" >

 

function validateItemNo(){
            if(totalSelectItem == 0){
                alert('Please select item to add in to the basket');
               
            } 
            else {
                alert(1);
                exeMe();
            }
            
}

 

<apex:commandLink onclick="validateItemNo();return false;"  >Add to Basket</apex:commandLink>
Shiv ShankarShiv Shankar
No it's not calling, but if i remove reRender than it calls
Avidev9Avidev9
Well then give it a try the workaround that I posted above