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
Mishu BhatMishu Bhat 

How can we show/hide a div based on a picklist value in lightning?

I want to show/hide a div that contains an input field, based on the picklist value in lightning:

CMP: 
<div class="slds-col">
            <lightning:select class="select-auto-width" name="selectEmailPreference" aura:id="selectEmailPreference" label="Current Email Preference" required="true" onchange="{!c.setEmailPreference}">
            <option value="1">No Email Updates</option>
            <option value="2">Send all Email Updates</option>
            <option value="3">Send Critical Updates and Fixes Only</option>
        </lightning:select>

Show this div only when the value is either option 2 or option 3
 <div class="slds-col">
                   <lightning:layoutItem size="4">
                    <lightning:input type="email" label="Current Email"/>
                       </lightning:layoutItem>
                   </div>
Best Answer chosen by Mishu Bhat
Maharajan CMaharajan C
Hi Mishu,

1. Create one more boolean attribute.
2. use the aura:if.


Component:

<aura:attribute name="showdiv" type="boolean" default="false" />           <!--   Change the default Value as per your need -->

<div class="slds-col">
            <lightning:select class="select-auto-width" name="selectEmailPreference" aura:id="selectEmailPreference" label="Current Email Preference" required="true" onchange="{!c.setEmailPreference}">
            <option value="1">No Email Updates</option>
            <option value="2">Send all Email Updates</option>
            <option value="3">Send Critical Updates and Fixes Only</option>
        </lightning:select>

            <aura:if isTrue="{!v.showdiv}"> 
 <div class="slds-col">
                   <lightning:layoutItem size="4">
                    <lightning:input type="email" label="Current Email"/>
                       </lightning:layoutItem>
                   </div>
           </aura:if>


JS Controller:

setEmailPreference
: function(component,helper,event){
        alert(component.find('selectEmailPreference
').get('v.value') + '   is Selected');
        var selectValue= component.find('selectEmailPreference').get('v.value');
        if(selectValue == '
2'  ||  selectValue =='3'){
            component.set("v.showdiv", true);
        }
        else{
            component.set("v.showdiv", false);
        }
    }


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Maharajan.C

 

All Answers

Maharajan CMaharajan C
Hi Mishu,

1. Create one more boolean attribute.
2. use the aura:if.


Component:

<aura:attribute name="showdiv" type="boolean" default="false" />           <!--   Change the default Value as per your need -->

<div class="slds-col">
            <lightning:select class="select-auto-width" name="selectEmailPreference" aura:id="selectEmailPreference" label="Current Email Preference" required="true" onchange="{!c.setEmailPreference}">
            <option value="1">No Email Updates</option>
            <option value="2">Send all Email Updates</option>
            <option value="3">Send Critical Updates and Fixes Only</option>
        </lightning:select>

            <aura:if isTrue="{!v.showdiv}"> 
 <div class="slds-col">
                   <lightning:layoutItem size="4">
                    <lightning:input type="email" label="Current Email"/>
                       </lightning:layoutItem>
                   </div>
           </aura:if>


JS Controller:

setEmailPreference
: function(component,helper,event){
        alert(component.find('selectEmailPreference
').get('v.value') + '   is Selected');
        var selectValue= component.find('selectEmailPreference').get('v.value');
        if(selectValue == '
2'  ||  selectValue =='3'){
            component.set("v.showdiv", true);
        }
        else{
            component.set("v.showdiv", false);
        }
    }


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Maharajan.C

 
This was selected as the best answer
Mishu BhatMishu Bhat
Thanks this worked!
suji srinivasansuji srinivasan
Hi maharajan, your code was helpful.