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
NickKahnNickKahn 

Collapse PageBlockSection by default on page load

This script works well if you are NOT using the `<apex:repeat`
 
<script>
    twistSection(document.getElementById('{!$Component.pb.pbs}').getElementsByTagName('img')[0])    
    </script>



Using with `<apex:repeat` does not work, I able to duplicate and here is the code:

How to collapse the `<apex:pageBlockSection>` on page load? I'm getting a `null` below is the code:

VFP:

   
<apex:page sidebar="false" controller="repeatCon">
      <apex:form >  
        <apex:repeat value="{!strings}" var="string" id="theRepeat">
               <apex:pageBlock title="hiding pb" mode="edit " id="pb">
                   <apex:pageBlockSection title="my title" columns="1" id="pbs"  > 
                        some text here...
                    </apex:pageBlockSection>   
                </apex:pageBlock> 
         </apex:repeat>
    
         <script>
             twistSection(document.getElementById('{!$Component.pb.pbs}').getElementsByTagName('img')[0])    
        </script>
        </apex:form> 
    </apex:page>



Apex:

   
public class repeatCon {
    
        public PageReference createNewJobDetailBtn() {
            return null;
        } 
        public String[] getStrings() {
          return new String[]{'ONE','TWO','THREE'};
     
        } 
    }

 
Lee Gregory 4Lee Gregory 4
You can use the script inside a repeat by embedding it inside the pageblocksection and adjusting the element id:

<apex:repeat>
    <apex:pageBlockSection title="Section" id="Sec1">
             <script>twistSection(document.getElementById('img_{!$Component.Sec1}'));</script>