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 

how to call javascript function based on button click

Hi All,
In my below code, I have to call tow javascript functions "amtValidation() and clickMe()" inside javascript "nameValidation". These functions should be called based on the button click. when "send" button is clicked it has to call amtValidation(), and when "click Me" is clicked it has to call clickMe(). Please help.
============
<apex:page controller="Bookings" id="page">
<script>
    
    function nameValidation(){
    
      var n = document.getElementById('{!$Component.page.form.pb.pbs.name}');
        alert(n.value);
        if(n.value == "")
        {
            alert("Name is mandatory");
            return false;
        }
        else{
        alert("enter name else");
         amtValidation();
         clickMe();
        }
        }
    function amtValidation(){
        //process here
    }
     function clickMe(){
        //process here
    }
    
    <apex:form id="form">
  <apex:actionFunction action="{!send}" name="send" reRender="pb" />
  <apex:actionFunction action="{!clickme}" name="clickme" reRender="pb"/>
  <apex:pageBlock title="Vendor Registration Form" id="pb">
  <apex:pageBlockSection columns="2" id="pbs">
  Name :<apex:inputText value="{!vcperson}" id="name"/>
  
   
  </apex:pageBlockSection>
  <apex:outputPanel id="op">
  <apex:commandButton value="Send" id="email" action="{!send}" onclick="return nameValidation();return amtValidation()"/>
  <apex:commandButton value="Click Me" id="pdf" action="{!clickme}" onclick="return nameValidation();return clickMe()"/>
  </apex:outputPanel>
  
  
  </apex:pageBlock>
</apex:form>
</apex:page>
Best Answer chosen by Ap30
mukesh guptamukesh gupta
Hi,

Please follow below code:
 
<apex:page controller="Bookings" id="page">
<script>
    
    function nameValidation(id){
    
	if(id == 'email'){
	this.amtValidation();
	}
	if(id == 'pdf'){
	this.clickMe();
	}
	
     /* var n = document.getElementById('{!$Component.page.form.pb.pbs.name}');
        alert(n.value);
        if(n.value == "")
        {
            alert("Name is mandatory");
            return false;
        }
        else{
        alert("enter name else");
         amtValidation();
         clickMe();
        }
        }
    function amtValidation(){
        //process here
    }
     function clickMe(){
        //process here
    }*/
    
    <apex:form id="form">
  <apex:actionFunction action="{!send}" name="send" reRender="pb" />
  <apex:actionFunction action="{!clickme}" name="clickme" reRender="pb"/>
  <apex:pageBlock title="Vendor Registration Form" id="pb">
  <apex:pageBlockSection columns="2" id="pbs">
  Name :<apex:inputText value="{!vcperson}" id="name"/>
  
   
  </apex:pageBlockSection>
  <apex:outputPanel id="op">
  <apex:commandButton value="Send" id="email" action="{!send}" onclick="nameValidation(this.id);"/>
  <apex:commandButton value="Click Me" id="pdf" action="{!clickme}" onclick="nameValidation(this.id);"/>
  </apex:outputPanel>
  
  
  </apex:pageBlock>
</apex:form>
</apex:page>

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi,

Please follow below code:
 
<apex:page controller="Bookings" id="page">
<script>
    
    function nameValidation(id){
    
	if(id == 'email'){
	this.amtValidation();
	}
	if(id == 'pdf'){
	this.clickMe();
	}
	
     /* var n = document.getElementById('{!$Component.page.form.pb.pbs.name}');
        alert(n.value);
        if(n.value == "")
        {
            alert("Name is mandatory");
            return false;
        }
        else{
        alert("enter name else");
         amtValidation();
         clickMe();
        }
        }
    function amtValidation(){
        //process here
    }
     function clickMe(){
        //process here
    }*/
    
    <apex:form id="form">
  <apex:actionFunction action="{!send}" name="send" reRender="pb" />
  <apex:actionFunction action="{!clickme}" name="clickme" reRender="pb"/>
  <apex:pageBlock title="Vendor Registration Form" id="pb">
  <apex:pageBlockSection columns="2" id="pbs">
  Name :<apex:inputText value="{!vcperson}" id="name"/>
  
   
  </apex:pageBlockSection>
  <apex:outputPanel id="op">
  <apex:commandButton value="Send" id="email" action="{!send}" onclick="nameValidation(this.id);"/>
  <apex:commandButton value="Click Me" id="pdf" action="{!clickme}" onclick="nameValidation(this.id);"/>
  </apex:outputPanel>
  
  
  </apex:pageBlock>
</apex:form>
</apex:page>

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Evelyn StevensonEvelyn Stevenson
<html>
<head>
<script type = "text/javascript" src="function.js"></script>
</head> myccpay (https://www.acesetm.biz/)
<body>
<p>Click the following button to see the function in action</p>
<input type = "button" onclick = "myfunction()" value = "Display">
</body>
paulpraveen paulpraveenpaulpraveen paulpraveen
Thanks for the clarification.
Ap30Ap30
Hi Mukesh, Thanks for the reply. I could able to call the functions now. All the validations worked fine except one. I have highlighted the part which is not working. Please guide me how to resolve this. Once the state and country field is entered , on clicking "click me" ,it comes to the else part, but pdf is not generated which was working sometime before. Name : Amount : State : Country :
mukesh guptamukesh gupta
Hi,

Where is your highlited code. Please share me

 
Ap30Ap30
I could able to call the functions now. All the validations worked fine except one. I have highlighted the part which is not working. Please guide me how to resolve this. Once the state and country field is entered , on clicking "click me" ,it comes to the else part, but pdf is not generated which was working sometime before. Name : Amount : State : Country :
mukesh guptamukesh gupta
Hi,

For pdf you need to add renderAs="pdf"
 
<apex:page controller="Bookings" id="page" renderAs="pdf">

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 
paulpraveen paulpraveenpaulpraveen paulpraveen
Thanks  get-mobdro.com (https://get-mobdro.com/home/)
tracy bergetracy berge
Donkey Kong (https://donkey-kong.io) game has since become a beloved classic and has been ported to many different platforms over the years.