5 <title>jQuery UI Tabs - Simple manipulation</title>
6 <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
7 <script src="../../jquery-1.9.1.js"></script>
8 <script src="../../ui/jquery.ui.position.js"></script>
9 <script src="../../ui/jquery.ui.core.js"></script>
10 <script src="../../ui/jquery.ui.widget.js"></script>
11 <script src="../../ui/jquery.ui.button.js"></script>
12 <script src="../../ui/jquery.ui.tabs.js"></script>
13 <script src="../../ui/jquery.ui.dialog.js"></script>
14 <link rel="stylesheet" href="../demos.css">
16 #dialog label, #dialog input { display:block; }
17 #dialog label { margin-top: 0.5em; }
18 #dialog input, #dialog textarea { width: 95%; }
19 #tabs { margin-top: 1em; }
20 #tabs li .ui-icon-close { float: left; margin: 0.4em 0.2em 0 0; cursor: pointer; }
21 #add_tab { cursor: pointer; }
25 var tabTitle = $( "#tab_title" ),
26 tabContent = $( "#tab_content" ),
27 tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
30 var tabs = $( "#tabs" ).tabs();
32 // modal dialog init: custom buttons and a "close" callback reseting the form inside
33 var dialog = $( "#dialog" ).dialog({
39 $( this ).dialog( "close" );
42 $( this ).dialog( "close" );
50 // addTab form: calls addTab function on submit and closes the dialog
51 var form = dialog.find( "form" ).submit(function( event ) {
53 dialog.dialog( "close" );
54 event.preventDefault();
57 // actual addTab function: adds new tab using the input from the form above
59 var label = tabTitle.val() || "Tab " + tabCounter,
60 id = "tabs-" + tabCounter,
61 li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
62 tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";
64 tabs.find( ".ui-tabs-nav" ).append( li );
65 tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
66 tabs.tabs( "refresh" );
70 // addTab button: just opens the dialog
74 dialog.dialog( "open" );
77 // close icon: removing the tab on click
78 tabs.delegate( "span.ui-icon-close", "click", function() {
79 var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
80 $( "#" + panelId ).remove();
81 tabs.tabs( "refresh" );
84 tabs.bind( "keyup", function( event ) {
85 if ( event.altKey && event.keyCode === $.ui.keyCode.BACKSPACE ) {
86 var panelId = tabs.find( ".ui-tabs-active" ).remove().attr( "aria-controls" );
87 $( "#" + panelId ).remove();
88 tabs.tabs( "refresh" );
96 <div id="dialog" title="Tab data">
98 <fieldset class="ui-helper-reset">
99 <label for="tab_title">Title</label>
100 <input type="text" name="tab_title" id="tab_title" value="" class="ui-widget-content ui-corner-all" />
101 <label for="tab_content">Content</label>
102 <textarea name="tab_content" id="tab_content" class="ui-widget-content ui-corner-all"></textarea>
107 <button id="add_tab">Add Tab</button>
111 <li><a href="#tabs-1">Nunc tincidunt</a> <span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>
114 <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
118 <div class="demo-description">
119 <p>Simple tabs adding and removing.</p>