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()" rerender="abc" />

 

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>

 

Did you ever try this workaround ?

Bhawani SharmaBhawani Sharma
If your javaScriptMethod returns true, your action will be get called. It it returns false, action will not be called.
~Onkar~Onkar

Hi

 

I am not sure about your business requirement.

 

But return statement javascript onclick is validate the form action of visualforce. If onclick return true javascript will submit the form and allow to call the action method from controller and if javascript return false it will not call the action method from controller.

 

It wll also check the button type from submit to normal button.

 

In your case you are using "reRender" which automatic add AJAX method in form and add default  return false.

 

Check code

VF

<apex:page >
<apex:form >
<apex:inputText id="testid"/>
<apex:commandButton action="controllerMethod()" onclick="return validateItemNo()" value="Click" reRender="testid"/>
</apex:form>

 

Client code generate by SFDC

<input name="j_id0:j_id1" value="j_id0:j_id1" type="hidden">
<input id="j_id0:j_id1:testid" name="j_id0:j_id1:testid" type="text">
<input class="btn" id="j_id0:j_id1:j_id2" name="j_id0:j_id1:j_id2"
onclick="return validateItemNo();A4J.AJAX.Submit('j_id0:j_id1',event,{'similarityGroupingId':'j_id0:j_id1:j_id2','parameters':{'j_id0:j_id1:j_id2':'j_id0:j_id1:j_id2'} } );return false;" value="Click" type="button">
<div id="j_id0:j_id1:j_id4"></div>

 

Now code without RenderAs

<apex:page >
<apex:form >
<apex:inputText id="testid"/>
<apex:commandButton action="controllerMethod()" onclick="return validateItemNo()" value="Click"/>
</apex:form>

 

Code generate by SFDC

<input name="j_id0:j_id1" value="j_id0:j_id1" type="hidden">
<input id="j_id0:j_id1:testid" name="j_id0:j_id1:testid" type="text">
<input name="j_id0:j_id1:j_id2" value="Click" onclick="return validateItemNo()" class="btn" type="submit">
<div id="j_id0:j_id1:j_id4"></div>

 

You can check its will work according to your way. But when you add "renderAs" Salesforce default will add false return and form will not submitted to controller.

 

~Thanks,

Onkar Kumar

Shiv ShankarShiv Shankar

Hi Onkar,

 

thanku very much to make my fundamental lilltle more clear by your valubel reply. 

Onkar but in my requirement i need to rerender the other bolcks, is there any alternative for that, so i can call the apex method also and can display error through java script and reRender also...

 

Thanks,