2
<apex:page standardController="Account">
<script type="text/javascript">
        function msgBox()
        {
            alert("hi");
        }
     </script>
    <apex:form id="frm">
        <apex:pageBlock title="My Content" mode="edit" id="pb">

            <apex:pageBlockSection title="My Content Section" columns="2" id="pbs">
                <apex:inputField value="{!account.name}" id="fst"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
             <apex:pageBlockButtons >
                <apex:commandButton onclick="msgbox();" value="Save"/>
            </apex:pageBlockButtons> 
        </apex:pageBlock>
    </apex:form>
</apex:page>
3

Add you script inside apex:form.. Also Javascript is case sensative so use same name in your msgBox.. Thanks to @Martin for notice..

And best practice is to add your script end of your HTML code.. So your page DOM should load faster.

<apex:page standardController="Account">

    <apex:form id="frm">
        <apex:pageBlock title="My Content" mode="edit" id="pb">

            <apex:pageBlockSection title="My Content Section" columns="2" id="pbs">
                <apex:inputField value="{!account.name}" id="fst"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
             <apex:pageBlockButtons >
                <apex:commandButton onclick="msgBox();" value="Save"/>
            </apex:pageBlockButtons> 
        </apex:pageBlock>

      <script type="text/javascript">
        function msgBox()
        {
            alert("hi");
        }
     </script>
    </apex:form>
</apex:page>
  • Hello ratan thanks.I just want to know what is need to write javascript inside form tag i saw code in which its wrote outside form tag and work properly. – Bonny Mar 3 '16 at 10:56
  • @Bonny ohh my mis.. Basically issue was related to your casesensative.. And yes it will be good practice to use script inside form – Ratan Paul Mar 3 '16 at 11:00

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.