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
Sylvie SerpletSylvie Serplet 

Hide and Show Expandable Section (slds markup) in VF page for displaying in LEX

Hi all,

I have a VF page using SLDS markup (I am in LEX) and I would like to implement expandable/collapsible sections in my form.

This is a code for one of my section with SLDS Expandable section https://www.lightningdesignsystem.com/components/expandable-section/
<div class="slds-form slds-form_compound">
                <fieldset class="slds-box slds-theme--default slds-container--medium slds-align_absolute-center"> 
                    <div class="slds-section">
                        <h3 class="slds-section__title">
                            <button class="slds-button slds-section__title-action" aria-controls="content" aria-expanded="false">
                                <svg aria-hidden="true" class="slds-section__title-action-icon slds-button__icon slds-button__icon--left" aria-hidden="true">
                                    <use xmlns:xlink="http://www.w3.org/1999/xlink" 
                                         xlink:href="/apexpages/slds/latest/assets/icons/utility-sprite/svg/symbols.svg#switch"></use>
                                </svg>XXXX</button>
                        </h3>
                        <div class="slds-section__content" id="content" aria-hidden="true" >
                            <div class="slds-form-element">
                                <label class="slds-form-element__label" >ABC</label>
                                <div class="slds-form-element__control">
                                    <apex:inputfield value="{!FF.ABC}" styleClass="slds-input"/>
                                </div>
                            </div>                 
                            <div class="slds-form-element">
                                <label class="slds-form-element__label" ">XYZ</label>
                                <div class="slds-form-element__control">
                                    <apex:inputfield value="{!FF.XYZ}" styleClass="slds-input"/>
                                </div>
                            </div> 
....
</fieldset>
            </div>
I have tried to implement this solution with jquery: http://www.minerva18.com/blog/creating-expandablecollapsible-lightning-design-sections-in-salesforce/#comment-1763 but the section open only for a few second and close again and the screen jump to the top of the page (I am using Chrome). 

I have also tried other solutions such as
<script type="text/javascript">
    function toggle_visibility() {
   var e = document.getElementById('content'); 
          if(e.style.display == 'none')
          e.style.display = 'block';
       else
          e.style.display = 'none';
    }
</script>

adding onclick="toggle_visibility();" on the button
or
function showContent() {
   {
        document.getElementById('content').style.visibility="visible";
    }
    else
    {
        document.getElementById('content').style.visibility="hidden";
    }
}
or
$(function () { 
    $('content').on('click', function (e) {
        var menuItem = $( e.currentTarget );

        if (menuItem.attr( 'aria-expanded') === 'true') {
            $(this).attr( 'aria-expanded', 'false');
            $(this).attr( 'aria-hidden', 'true')
        } else {
            $(this).attr( 'aria-expanded', 'true');
            $(this).attr( 'aria-hidden', 'false')
        }
    });
});

using jquery: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

But nothing is working.
Any help will be greatly appreciated!
Sylvie
Best Answer chosen by Sylvie Serplet
Gaurish Gopal GoelGaurish Gopal Goel
Hi Sylvie,

You just need simple JavaScript. Try this simple code in your VF page, it works in Lightning perfectly. You can now apply your own CSS according to your requirement. Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.​
<apex:page >
    <script>
        function show(obj)
        {
            if(document.getElementById(obj).style.display == 'none')
            {
                document.getElementById(obj).style.display = 'block';
            }
            else
                document.getElementById(obj).style.display = 'none';
        }
    </script>
    
    <div>
        <a href="javascript:;" onclick="show(this.nextSibling.nextSibling.id);">Show/Hide Text</a>
        <div id="expand">
            This is an example for expandible and collapsible sections using JS
        </div>
    </div>
    <br/>
    <div>
        <a href="javascript:;" onclick="show(this.nextSibling.nextSibling.id);">Show/Hide Text1</a>
        <div id="expand1">
            This is an example for expandible and collapsible sections using JS
        </div>
    </div>
</apex:page>
Regards,
Gaurish

All Answers

sumithasumitha
Hi Sylvie,
      Try the below code it may work
<div id="accordion">
       <h3></h3>
  <div>
     <!--content-->
  </div>
  <h3></h3>
  <div>
      <!--content-->
  </div>
  <h3></h3>
  <div>
     <!-- content-->
</div>
  <script>
  $( function() {
    $( "#accordion" ).accordion({
      collapsible: true
    });
  } );
  </script>

Thanks,
Sumitha P

 
Gaurish Gopal GoelGaurish Gopal Goel
Hi Sylvie,

You just need simple JavaScript. Try this simple code in your VF page, it works in Lightning perfectly. You can now apply your own CSS according to your requirement. Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.​
<apex:page >
    <script>
        function show(obj)
        {
            if(document.getElementById(obj).style.display == 'none')
            {
                document.getElementById(obj).style.display = 'block';
            }
            else
                document.getElementById(obj).style.display = 'none';
        }
    </script>
    
    <div>
        <a href="javascript:;" onclick="show(this.nextSibling.nextSibling.id);">Show/Hide Text</a>
        <div id="expand">
            This is an example for expandible and collapsible sections using JS
        </div>
    </div>
    <br/>
    <div>
        <a href="javascript:;" onclick="show(this.nextSibling.nextSibling.id);">Show/Hide Text1</a>
        <div id="expand1">
            This is an example for expandible and collapsible sections using JS
        </div>
    </div>
</apex:page>
Regards,
Gaurish
This was selected as the best answer
Alain CabonAlain Cabon
Hello Sylvie,

1) CREATING EXPANDABLE / COLLAPSIBLE LIGHTNING DESIGN SECTIONS IN SALESFORCE
AUGUST 13, 2016 - RAGHAVENDHRA YARLAGADDA

http://www.minerva18.com/blog/creating-expandablecollapsible-lightning-design-sections-in-salesforce/


2) jQuery UI could also help you.

https://jqueryui.com/accordion/
https://www.tutorialspoint.com/jqueryui/jqueryui_accordion.htm

 
Sylvie SerpletSylvie Serplet
Thank you Gaurish,
It works great.
However I have to remove the button and put the link instead. Also added some markup to make it collapsed when opening the form.
Sylvie.
Gaurish Gopal GoelGaurish Gopal Goel
@Sylvie: Good to hear that it helped. Happy coding.
K MamataK Mamata

@sylvie and @gaurish
I am referring to your comments for one of my similar requirements. Thanks it helped a lot. Further to this, Is there any way to change the text? Instead of showing 'show/hide text' to click., I want to show 'Show text' and when the text is shown, it should give 'Hide text' link.
Thansk in advance !!
Thanks,
Mamata