0

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

<script>
  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?

2
  • 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

4

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
  • 1
    Thanks sfdcfox. Seems so obvious and simple once you know the trick.
    – Ohmicron
    Apr 19, 2017 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .