[SalesForce] how to hide and show the pageblock using jquery in vf page

I have a visualforce page.based on command link click i want to disaply different pageblock.I want to achieve this using jquery.
Please guide me

vf:

   <apex:page standardController="Contact" extensions="getAccountID_ctrl" recordSetVar="">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<apex:form >

<script>

 function toggleDiv() {

       $("[id$=a]").toggle();

   }

   function toggleDiv1() {
        $("[id$=b]").toggle();
   }
</script>

<apex:pageBlock >
<a href="#" onclick="toggleDiv()">Uma</a>
<a href="#" onclick="toggleDiv1()">NewTable</a>
</apex:pageBlock>


<apex:outputPanel id="a"  style="display:none;">
    <apex:pageblock id="a1">
    <apex:outputLabel >UmaValue</apex:outputLabel>
    </apex:pageBlock>
</apex:outputPanel>  

<apex:outputPanel id="b" style="display:none;">
    <apex:pageblock id="b1">
    <apex:outputLabel >NewTableValue</apex:outputLabel>
    </apex:pageBlock>
</apex:outputPanel>

</apex:form>
</apex:page>

Best Answer

Looks like you forgot to add jquery file in page

Add this. It will work for you

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

Also apex:commandLink will reload your VF page. I suggest you use HTML anchor tag

replace this

<apex:commandLink value="Uma" id="uma" onclick="toggleDiv()"/>
<apex:commandLink value="NewTable" id="check"/>

to

<a href="#" onclick="toggleDiv()">Uma</a>

Updates:

<apex:page standardController="Contact"  recordSetVar="">
   <apex:form >
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
      <script>
         function toggleDiv() {
                $("[id$=op]").toggle();
                $("[id$=oi]").toggle();
                $("[id$=oplink]").toggle();
                $("[id$=oilink]").toggle();
           }
      </script>
      <apex:pageBlock >
         Id: {!$CurrentPage.parameters.id}
         <a href="#" onclick="toggleDiv()" id="oplink" >Uma </a> 
         <a href="#" onclick="toggleDiv()" id="oilink" style="display:none;"> NewTable</a> 

      </apex:pageBlock>
      <apex:outputPanel id="op" >
         <apex:pageblock >
            <apex:outputLabel >UmaValue</apex:outputLabel>
         </apex:pageBlock>
      </apex:outputPanel>
      <apex:outputPanel id="oi" style="display:none;">
         <apex:pageblock >
            <apex:outputLabel >NewTableValue</apex:outputLabel>
         </apex:pageBlock>
      </apex:outputPanel>
   </apex:form>
</apex:page>
Related Topic