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
Debendra Ray 8Debendra Ray 8 

Javascript not called from Visualforce Page

Hi All,
The javascript in the below code is not getting invoked :
<apex:page standardController="Customer_Visit_Report__c" recordSetVar="Customer_Visit_Report__c" tabStyle="Customer_Visit_Report__c" extensions="cvrController"> 
 <apex:sectionHeader title="Customer Visit Report" subtitle="{!cvr.Name}"/>
 <apex:pageMessages id="pageMessages"/> 

    <script type="text/javascript">

    function validate()
    {
        alert("Test Alert --->");
        if(((document.getElementById('{!$Component.CVRFrm.invt.therepeatINV.Contact}').value == '') && 
           (document.getElementById('{!$Component.CVRFrm.invt.therepeatINV.User}').value == '') )   ||
           (document.getElementById('{!$Component.CVRFrm.invt.therepeatINV.Organisation}').value == '' )
          )
        {
            alert("Please add Invitee Details for the CVR !");
        }
      
        if(
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatAP }').value == '')  ||
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatDP}').value == '')   ||
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatATBT}').value == '') ||
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatAB}').value == '')   ||
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatCD}').value == '')   ||
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatBU}').value == '')   ||
          (document.getElementById('{!$Component.CVRFrm.actPnt.therepeatST}').value == '')  
         ) 
        {
            alert("Please add Action Items for the CVR!");
        }
        else
        {
            Save();
            alert("CVR is created successfully!");
        }
        
        
    }
    
    </script>
    
 <apex:form id="CVRFrm"> 
     
     <apex:pageBlock mode="edit"> 
          <apex:pageBlockButtons >          
            <apex:commandButton value="Save" action="{!save}" onclick="validate()"/> 
            <apex:commandButton value="Cancel" action="{!cancel}"/> 
          </apex:pageBlockButtons>   
         
        <apex:pageBlockSection title="Minutes of Meeting">
            <apex:inputField value="{!cvr.Name}"  required="true"/> 
            <apex:inputField value="{!cvr.Date_of_Visit__c}"  required="true"/> 
            <apex:inputField value="{!cvr.Start_Time__c}"  required="true"/> 
            <apex:inputField value="{!cvr.End_Time__c}"  required="true"/> 
            <apex:inputField value="{!cvr.Discussion_Reference__c}"  required="true"/>  
            <apex:inputField value="{!cvr.Venue__c}"  required="true"/> 
            <apex:inputField value="{!cvr.Type__c}"  required="true"/> 
            <apex:inputField value="{!cvr.Agenda__c}"  required="true"/>
            <apex:inputField value="{!cvr.Account__c}"  required="false"/>  
            <apex:inputField value="{!cvr.Lead__c}"  required="false"/> 
            <apex:inputField value="{!cvr.Opportunity__c}"/>  
         </apex:pageBlockSection>
     </apex:pageBlock> 
     
     <apex:pageBlock mode="edit"> 
         <apex:pageBlockButtons >
                    <apex:commandbutton value="Add Invitee" action="{!AddInvitee}" rerender="invt"/>
                    <apex:commandButton value="Remove Invitee" action="{!DelInvitee}" rerender="invt"/>                                            
          </apex:pageBlockButtons>
           
         <apex:pageBlockSection title="Invitee" id="invt"> 
             <apex:repeat value="{!lstInvitee}" var="inv" id="therepeatINV">
                 <apex:pageMessage summary="Please select either Invitee (Internal) or Invitee (External). Only one can be filled at a time." severity="error" strength="3" /> 
                 <apex:pageMessages />              
                 <apex:inputField value="{!inv.Contact__c}" id="Contact"/>
                 <apex:inputField value="{!inv.User__c}" id="User"/> 
                
                 <!-- <apex:inputField value="{!inv.Invitee_Name__c}"/> -->
                 <apex:inputField value="{!inv.Organization__c}" id="Organisation"/> 
             </apex:repeat>
             
         </apex:pageBlockSection>
   </apex:pageBlock>  
  <!--   
   <apex:pageBlock mode="edit"> 
         <apex:pageBlockButtons >
                    <apex:commandbutton value="Add Agenda" action="{!AddAgenda}" rerender="agend"/>
                    <apex:commandButton value="Remove Agenda" action="{!DelAgenda}" rerender="agend"/>                                            
          </apex:pageBlockButtons>
           
         <apex:pageBlockSection title="Agenda" id="agend"> 
             <apex:repeat value="{!lstAgenda}" var="agd" id="therepeatAG"> 
                    <apex:inputField value="{!agd.Discussion_Topics__c}"/>
             </apex:repeat>
            
         </apex:pageBlockSection> 
        
   </apex:pageBlock>
   
   -->
     
   <apex:pageBlock mode="edit"> 
         <apex:pageBlockButtons >
                    <apex:commandbutton value="Add Action Points" action="{!AddActionPoints}" rerender="actPnt"/>
                    <apex:commandButton value="Remove Action Points" action="{!DelActionPoints}" rerender="actPnt"/>                                            
          </apex:pageBlockButtons>
           
         <apex:pageBlockSection title="Action Points" id="actPnt"> 
             <apex:repeat value="{!lstActionPoint}" var="acPt" id="therepeatAP"> 
                    <apex:inputField value="{!acPt.Discusssion_Point__c}" id="therepeatDP"/>
                    <apex:inputField value="{!acPt.Action_To_Be_Taken__c}" id="therepeatATBT"/>
                    <apex:inputField value="{!acPt.Action_By__c}" id="therepeatAB"/> 
                    <apex:inputField value="{!acPt.Completion_Date__c}" id="therepeatCD"/> 
                    <apex:inputField value="{!acPt.BU__c}" id="therepeatBU"/>                    
                    <apex:inputField value="{!acPt.Status__c}" id="therepeatST"/>  
             </apex:repeat>
             
         </apex:pageBlockSection>
   </apex:pageBlock>   
        
     
 </apex:form> 
    
   
</apex:page>

Please could you help identify the issue.

Regards,
​Debendra
 
NagendraNagendra (Salesforce Developers) 
Hi Debendra,

This is a recurring issue with the VF pages. You can use jQuery to solve this problem. Replace your document.getElementById statements by the corresponding jQuery ones.

For example,
document.getElementById('{!$Component.CVRFrm.actPnt.therepeatDP}').value     gets replaced by
$("[id='therepeatDP']").val()
Do this for all the statements and you'll be fine.

Do not forget to include jQuery in your static resource and refer it on your VF page.

Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra