Hi Saloni,
I just moved it out as separate function and calling it from commandbutton. you can do the same with commandlink as well.
<apex:page >
<script>
function expandCollapse(){
twistSection(document.getElementById(‘{!$Component.theForm.block1.section1}’).getElementsByTagName(‘img’)[0]);
}
</script>
<apex:form id=”theForm”>
<apex:commandButton value=”Click Me” onclick=”expandCollapse(); return false;”/>
<apex:pageBlock id=”block1″>
<apex:pageBlockSection id=”section1″ columns=”2″ collapsible=”true” title=”Title”>
Example Area
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Thanks.