5
<title>Example: Dynamic TabView from JavaScript</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Dynamic TabView from JavaScript</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><div class="intro">
23
<p>This example shows how to create a <code>TabView</code> widget dynamically and insert it into the page.</p>
26
<div class="example yui3-skin-sam">
30
<script type="text/javascript">
31
YUI().use('tabview', function(Y) {
32
var tabview = new Y.TabView({
35
content: '<p>foo content</p>'
38
content: '<p>bar content</p>'
41
content: '<p>baz content</p>'
45
tabview.render('#demo');
46
tabview.selectChild(2);
52
<h2>Creating a TabView From JavaScript</h2>
54
<p>A <code>TabView</code> can be created dynamically using a small amount of JavaScript.</p>
58
<p>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.</p>
60
<pre class="code prettyprint"><div id="demo">
61
</div></pre>
64
<h3>The JavaScript</h3>
66
<p>The minimal requirement for creating a <code>TabView</code> from scratch are the labels and content for each tab.
67
These are added as the <code>children</code> attribute when creating the <code>TabView</code>.</p>
69
<pre class="code prettyprint">var tabview = new Y.TabView({
71
label: 'foo',
72
content: '<p>foo content</p>'
74
label: 'bar',
75
content: '<p>bar content</p>'
77
label: 'baz',
78
content: '<p>baz content</p>'
85
<p>Calling render creates the <code>TabView</code>, inserting into the node
89
<pre class="code prettyprint">tabview.render('#demo');</pre>
92
<h3>Changing the Selected Tab</h3>
94
<p>By default, clicking a tab makes it selected and shows its content. You can also do this programmatically
95
by calling the <code>selectChild</code> method on the <code>TabView</code>, passing it the index of the
99
<pre class="code prettyprint">tabview.selectChild(2);</pre>
102
<h2>Complete Example Source</h2>
103
<pre class="code prettyprint"><div id="demo">
106
<script type="text/javascript">
107
YUI().use('tabview', function(Y) {
108
var tabview = new Y.TabView({
110
label: 'foo',
111
content: '<p>foo content</p>'
113
label: 'bar',
114
content: '<p>bar content</p>'
116
label: 'baz',
117
content: '<p>baz content</p>'
121
tabview.render('#demo');
122
tabview.selectChild(2);
124
</script></pre>
130
<div class="yui3-u-1-4">
131
<div class="sidebar">
135
<div class="sidebox">
137
<h2 class="no-toc">Examples</h2>
141
<ul class="examples">
144
<li data-description="This example shows how to create a TabView wigdet from existing HTML.">
145
<a href="tabview-basic.html">TabView from Existing Markup</a>
150
<li data-description="This example shows how to create a TabView wigdet from JavaScript.">
151
<a href="tabview-fromjs.html">Dynamic TabView from JavaScript</a>
156
<li data-description="This example shows how to add and remove Tabs.">
157
<a href="tabview-add-remove.html">Adding and Removing Tabs</a>
162
<li data-description="This example shows how to load tab content remotely using a YQL plugin.">
163
<a href="tabview-yql.html">Loading Tab Content</a>
175
<div class="sidebox">
177
<h2 class="no-toc">Examples That Use This Component</h2>
181
<ul class="examples">
192
<li data-description="Demonstrates how to add browser history support to a TabView widget using the History Utility.">
193
<a href="../history/history-tabview.html">History + TabView</a>
206
<script src="../assets/vendor/prettify/prettify-min.js"></script>
207
<script>prettyPrint();</script>