Event for changed selected tab

Hello!

How can I call some javascript function only when the second tab is selected?

Thanks!

Bernard.


3 Replies

SK Satheesh Kumar Balasubramanian Syncfusion Team May 23, 2022 01:16 PM UTC

Hi Bernard,

You can use Selecting and Selected event to achieve your requirement.

Index.cshtml:

<ejs-tab id="defaultTab" Selected="tabSelected" Selecting="tabSelecting">
    <e-tab-tabitems>
        <e-tab-tabitem header="ViewBag.headerTextOne" content="@contentOne"></e-tab-tabitem>
        <e-tab-tabitem header="ViewBag.headerTextTwo" content="@contentTwo"></e-tab-tabitem>
        <e-tab-tabitem header="ViewBag.headerTextThree" content="@contentThree"></e-tab-tabitem>
    </e-tab-tabitems>
</ejs-tab>

<script type="text/javascript">

    function tabSelected(args) {
        if (args.selectedIndex === 1) {
            // You can call javascript function
        }
    }
    function tabSelecting(args) {
        if (args.selectingIndex === 1) {
           // You can call javascript function
        }
    }
</script>

Kindly try the attached sample and let us know if this meets your requirement.

Regards,
Satheesh Kumar B

Attachment: AspCoreTab_d7eff2f7.zip


BJ Bernard Jurlina May 23, 2022 01:22 PM UTC

Hi Satheesh Kumar B,

and thanks for the answer. But, how can I set the events if I define the tab like this?




Regards,
Bernard.



SK Satheesh Kumar Balasubramanian Syncfusion Team May 24, 2022 10:39 AM UTC

Hi Bernard,

You can use selecting and selected event like below to achieve your requirement.


index.js:

var tabObj = new ej.navigations.Tab({
  heightAdjustMode: 'Auto',
  selecting: tabSelecting,
  selected: tabSelected,
});

//Render initialized Tab component
tabObj.appendTo('#ej2Tab');

function tabSelected(args) {
  if (args.selectedIndex === 1) {
    // You can call javascript function
  }
}
function tabSelecting(args) {
  if (args.selectingIndex === 1) {
    // You can call javascript function
  }
}

Kindly try the above sample and let us know if this meets your requirement.

Regards,
Satheesh Kumar B

Loader.
Up arrow icon