YUI 3.x Home -

YUI Library Examples: TabView: Dynamic TabView from JavaScript

TabView: Dynamic TabView from JavaScript

This example shows how to create a TabView widget dynamically and insert it into the page.

Creating a TabView From JavaScript

A TabView can be created dynamically using a small amount of JavaScript.

The Markup

There are no markup requirements in this case, although you may want to have a placeholder to render the tabview into, which is what this example does.

  1. <div id="demo">
  2. </div>
<div id="demo">
</div>

The JavaScript

The minimal requirement for creating a TabView from scratch are the labels and content for each tab. These are added as the children attribute when creating the TabView.

  1. var tabview = new Y.TabView({
  2. children: [{
  3. label: 'foo',
  4. content: '<p>foo content</p>'
  5. }, {
  6. label: 'bar',
  7. content: '<p>bar content</p>'
  8. }, {
  9. label: 'baz',
  10. content: '<p>baz content</p>'
  11. }]
  12. });
    var tabview = new Y.TabView({
        children: [{
            label: 'foo',
            content: '<p>foo content</p>'
        }, {
            label: 'bar',
            content: '<p>bar content</p>'
        }, {
            label: 'baz',
            content: '<p>baz content</p>'
        }]
    });

Rendering

Calling render creates the TabView, inserting into the node passed to render.

  1. tabview.render('#demo');
    tabview.render('#demo');

Changing the Selected Tab

By default, clicking a tab makes it selected and shows its content. You can also do this programmatically by calling the selectChild method on the TabView, passing it the index of the tab to be selected.

  1. tabview.selectChild(2);
    tabview.selectChild(2);

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings