Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically select a tab?

I am using a mdl tabbar from the layout component page.

  <header class="mdl-layout__header">
    <div class="mdl-layout__header-row">
      <!-- Title -->
      <span class="mdl-layout-title">Title</span>
    </div>
    <!-- Tabs -->
    <div class="mdl-layout__tab-bar mdl-js-ripple-effect">
      <a href="#fixed-tab-1" class="mdl-layout__tab is-active">Tab 1</a>
      <a href="#fixed-tab-2" class="mdl-layout__tab">Tab 2</a>
      <a href="#fixed-tab-3" class="mdl-layout__tab">Tab 3</a>
    </div>
  </header>

Do I have to add/remove the "is-active" class on both tab / panel or is there a simpler way to select a tab programmatically?

like image 309
dflorey Avatar asked Jul 12 '15 09:07

dflorey


People also ask

How do I select a tab?

Hold down the Shift key to select multiple tabs in a row or Ctrl / ⌘ key to select tabs that aren't necessarily next to each other.

How do I select a tab in jquery?

var selectedTab = $("#TabList"). tabs(). data("selected. tabs");


3 Answers

You can simulate a Click event by using the Click() method on a DOM Element.

document.getElementById("AnchorTagId").click()

like image 192
De Wet Ellis Avatar answered Dec 20 '22 18:12

De Wet Ellis


you can simulate a click in the tab button with jquery

ex (programmatically active the second tab - index 1):

$(".mdl-layout__tab:eq(1) span").click ();

ex (programmatically active the first tab - index 0):

$(".mdl-layout__tab:eq(0) span").click ();
like image 38
Fabio de Toledo Avatar answered Dec 20 '22 20:12

Fabio de Toledo


with mdl (@version v1.2.1)

you can simulate a click event with jQuery :

$(".mdl-tabs__tab:eq(0) span").click (); //for the first tab (index 0)
$(".mdl-tabs__tab:eq(1) span").click (); //for the second tab (index 1)

...

Tested on Firefox 50.0 - 50.0.2 and Microsoft Edge 38.14393.0.0

like image 45
damien Avatar answered Dec 20 '22 20:12

damien