Mar 26, 2025
247 Views

Control Salesforce Lightning Tabs With Next & Back Buttons

Written by

Hello, In this post, we are going to create a sample lightning component to control <lightning:tab> with Next and Back buttons.

In this code we are using the lightning:tab and lightning:tabset standard component for creating tabs in a Salesforce Lightning Component.

Lightning Component : LightningTabs.cmp

<aura:component >
    <aura:attribute name="selTabId" type="string" default="1" />    
    <lightning:tabset selectedTabId="{!v.selTabId}" >
        <lightning:tab label="Fruits" id="1">
            <p>Apple</p>
            <p>Banana</p>
            <p>Mango</p>
        </lightning:tab>        
        <lightning:tab label="Vegetables" id="2">
            <p>Potato</p>
            <p>Tomato</p>
            <p>Carrots</p>
        </lightning:tab>        
        <lightning:tab label="Colors" id="3">
            <p>Red</p>
            <p>Green</p>
            <p>Blue</p>
        </lightning:tab>       
         <lightning:tab label="Cars" id="4">
            <p>BMW</p>
            <p>Maserati</p>
            <p>Audi</p>
        </lightning:tab>        
    </lightning:tabset>
    <div class="slds-clearfix">
      <div class="slds-float_left">
          <!--disabled the back button on first Tab-->    
        <lightning:button disabled="{!v.selTabId == '1'}" variant="neutral" label="Back" onclick="{!c.back}" />
      </div>
      <div class="slds-float_right">
        <lightning:button variant="brand" label="Next" onclick="{!c.next}" />
      </div>
    </div>   
</aura:component>

JavaScript Controller : LightningTabsController.js

({
  next : function(component, event, helper) {
      // get the current selected tab value
      var currentTab = component.get("v.selTabId");       
      if(currentTab == '1'){
          component.set("v.selTabId" , '2');   
      }else if(currentTab == '2'){
          component.set("v.selTabId" , '3');     
      }else if(currentTab == '3'){
          component.set("v.selTabId" , '4');             
      }else if(currentTab == '4'){
          alert('Complete !');  
      } 
  },   
  back : function(component, event, helper) {
      // get the current selected tab value  
      var currentTab = component.get("v.selTabId");        
      if(currentTab == '2'){
          component.set("v.selTabId" , '1');     
      } else if(currentTab == '3'){
          component.set("v.selTabId" , '2');     
      } else if(currentTab == '4'){
          component.set("v.selTabId" , '3');     
      }
  }
})

demo.app [Lightning Application]

<aura:application extends="force:slds">
    <c:LightningTabs/>
    <!-- here c: is org. default namespace prefix-->
</aura:application>
Article Categories:
Blog
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.