I have a apex function that does not seem to care about the javascript return value. Why?

  function validate() {
    var result = false;
    var inp = document.querySelector('[id$="srchText"]').value;
    if(inp.length < 3) { showErrMsg(); }
    if(inp.length > 2) { showSrchResults(); result=true; }
    return result;

<apex:commandLink value="Search Cases" styleClass="btn" style="text-decoration:none;padding:5px;" onclick="validate();" action="{!srchCases}"/>

If the return result is false the action should not be called. If right after the onclick="validate();" I put return false ie (onclick="validate(); return false;" the apex action method does not get called. Why does the commandlink onclick action ignore the return results from the javascript and how do I get it to accept the return results?

  • why dont you use actionFunction? Apr 19, 2017 at 19:36
  • The end result if the javascript validates does call an actionfunction (apex method)
    – Ohmicron
    Apr 19, 2017 at 19:42

1 Answer 1


The inline event handler is a function in its own right; you have to return the value back if you want to prevent the default action:

onclick="return validate();"
  • 1
    Thanks sfdcfox. Seems so obvious and simple once you know the trick.
    – Ohmicron
    Apr 19, 2017 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.