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
Ap30Ap30 

return false is not working in javascript

Hi All,
In my below code, return false is not working. I have highlighted those lines. Though i'm getting alert message "Enter state...." , command button is executed. Any help is appreciated.
=============
<apex:page controller="Bookings" id="page">
<script>
    
    function nameValidation(id){
        
        var n = document.getElementById('{!$Component.page.form.pb.pbs.name}');
        
        if(n.value == "")
        {
            alert("Name is mandatory");
            return false;
        }
       
       
        if(id == 'page:form:pb:pdf'){
        this.clickMe();
        }
        }
function clickMe()
     {
        var a = document.getElementById('{!$Component.page.form.pb.pbs.state}');
        var b = document.getElementById('{!$Component.page.form.pb.pbs.coun}');
        var pdfbtn = document.querySelector("[id$='pdf']");
        
        if(a.value == "" )
        {
            alert("Enter state to show Generate Pdf Button");
            pdfbtn.style.visibility = 'hidden';
            return false;
        }
        else if(b.value == "")
        {
            alert("Enter Country to show Generate Pdf Button");
            pdfbtn.style.visibility = 'hidden';
            return false;
        }
        else
        {
            alert("inside else");
            clickme();
        }
    }
</script>
 <apex:form id="form">
    <apex:actionFunction action="{!clickme}" name="clickme" reRender="pb"/>
  <apex:actionFunction name="toggleButton" reRender="op"/>
  <apex:pageBlock title="Booking Form" id="pb">
  <apex:pageBlockSection columns="2" id="pbs">
  Name :<apex:inputText value="{!person}" id="name" />
  State :<apex:inputText value="{!state}" id="state" onchange="toggleButton()"/>
  Country :<apex:inputText value="{!country}" id="coun" onchange="toggleButton()"/>
  </apex:pageBlockSection>
  <apex:outputPanel id="op">
  <apex:commandButton value="Click Me" id="pdf" action="{!clickme}" onclick="return nameValidation(this.id);"/>
  
  </apex:outputPanel>
  </apex:pageBlock>
  
  </apex:form>
</apex:page>
Best Answer chosen by Ap30
Maharajan CMaharajan C
Hi,

Why you are returing false in JS? Is it to avoid the apex method call on button click.

If you want to avoid the method calling from button click simply put the false like below, and also remove the action from button.
<apex:commandButton value="Click Me" id="pdf" onclick="nameValidation(this.id);return false;"/>

Remove all the return false in JS.

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi,

Why you are returing false in JS? Is it to avoid the apex method call on button click.

If you want to avoid the method calling from button click simply put the false like below, and also remove the action from button.
<apex:commandButton value="Click Me" id="pdf" onclick="nameValidation(this.id);return false;"/>

Remove all the return false in JS.

Thanks,
Maharajan.C
This was selected as the best answer
Ap30Ap30
Thanks a lot. It worked.
Baya AdamBaya Adam
Information Transformation Services is endowing the clients with a stunning and impressive visual experience crafted by 3D Modeling Services. We are completely forted to offers our customers with a range of appealing 3D designs that are carefully crafted to meet with all type of requirements. 
 
Sanjay Vinayak TSanjay Vinayak T
Hi Ap30,

did you find a solution to disable the save button and generate a pdf for the vendor registration Visual force page?
if found can you share your code it will be so helpful for me to solve the problem.