Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public method setTab to set the active tab programmatically #5091

Merged
merged 1 commit into from Jun 27, 2017

Conversation

eKoopmans
Copy link
Contributor

  • Allows the active tab to be set programmatically, by element or index
  • This previously had to be accomplished by invoking .click() on the chosen tab element

Closes #5068

@Garbee
Copy link
Collaborator

Garbee commented Jun 27, 2017

LGTM thank you!

@Garbee Garbee merged commit 20b941b into google:mdl-1.x Jun 27, 2017
@toby-1-kenobi
Copy link

I can't find any documentation on how to use this.

@eKoopmans
Copy link
Contributor Author

Hey @toby-1-kenobi, good point - here's some documentation. Given the following HTML:

<div class="mdl-tabs mdl-js-tabs" id="myTabs">
  <div class="mdl-tabs__tab-bar">
    <a href="#panel1" class="mdl-tabs__tab is-active" id="firstTab">Panel 1</a>
    <a href="#panel2" class="mdl-tabs__tab>Panel 2</a>
  </div>
  <div class="mdl-tabs__panel is-active" id="panel1">
    <p>Panel 1 contents</p>
  </div>
  <div class="mdl-tabs__panel is-active" id="panel2">
    <p>Panel 2 contents</p>
  </div>
</div>

You can use setTab to change the tab programmatically, as follows:

var myTabs = document.getElementById('myTabs');
var mdlTabs = myTabs.MaterialTabs;

// Change focus to the second tab, using index.
mdlTabs.setTab(1);

// Change focus back to the first tab, using the tab element.
mdlTabs.setTab(document.getElementById('firstTab'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants