jQuery UI & Tabs: Vertical tabs

This entry is part 2 of 7 in the series jQuery UI Tabs

jQuery UI Tabs – Vertical Tabs functionality

Click tabs to swap between content that is divided into logical sections.

<!doctype html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>jQuery UI Tabs - Vertical Tabs functionality</title>
    <link rel='stylesheet' href='//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <script src='//code.jquery.com/ui/1.10.4/jquery-ui.js'></script>
    <link rel='stylesheet' href='https://codecrawl.com/code/jqueryui/jqueryui_style.css'>
    <script>
        $(function () {
            $('#tabs').tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
            $('#tabs li').removeClass('ui-corner-top').addClass('ui-corner-left');
        });
    </script>
    <style>
        .ui-tabs-vertical {
            width: 55em;
        }

        .ui-tabs-vertical .ui-tabs-nav {
            padding: .2em .1em .2em .2em;
            float: left;
            width: 12em;
        }

        .ui-tabs-vertical .ui-tabs-nav li {
            clear: left;
            width: 100%;
            border-bottom-width: 1px !important;
            border-right-width: 0 !important;
            margin: 0 -1px .2em 0;
        }

        .ui-tabs-vertical .ui-tabs-nav li a {
            display: block;
        }

        .ui-tabs-vertical .ui-tabs-nav li.ui-tabs-active {
            padding-bottom: 0;
            padding-right: .1em;
            border-right-width: 1px;
            border-right-width: 1px;
        }

        .ui-tabs-vertical .ui-tabs-panel {
            padding: 1em;
            float: right;
            width: 40em;
        }
    </style>
</head>
<body>

<div id='tabs'>
    <ul>
        <li><a href='#tabs-1'>Pitbull</a></li>
        <li><a href='#tabs-2'>Sean Kingston</a></li>
        <li><a href='#tabs-3'>Taylor Swift</a></li>
    </ul>
    <div id='tabs-1'>
        <h2>Content heading 1</h2>

        <p>Pitbull is a Cuban–American recording artist and Latin Grammy winning rapper from Miami, Florida.</p>
    </div>
    <div id='tabs-2'>
        <h2>Content heading 2</h2>

        <p>Sean Kingston pursued a music career and debuted in 2007 with the album Sean Kingston.</p>
    </div>
    <div id='tabs-3'>
        <h2>Content heading 3</h2>

        <p>Taylor Swift, raised in Wyomissing, Pennsylvania. She moved to Nashville, Tennessee at the age of
            fourteen.</p>

        <p>She is known for her narrative songs about her personal experiences.</p>
    </div>
</div>

</body>
</html>
Try The Code

jQuery UI & Tabs: Tabs at bottom

This entry is part 4 of 7 in the series jQuery UI Tabs

JS Bin

With some additional CSS (for positioning) and JS (to put the right classes on elements) the tabs can be placed below their content.

<!doctype html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>jQuery UI Tabs - Tabs at bottom</title>
    <link rel='stylesheet' href='//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <script src='//code.jquery.com/ui/1.10.4/jquery-ui.js'></script>
    <link rel='stylesheet' href='https://codecrawl.com/code/jqueryui/jqueryui_style.css'>
    <script>
        $(function () {
            $('#tabs').tabs();

            // fix the classes
            $('.tabs-bottom .ui-tabs-nav, .tabs-bottom .ui-tabs-nav > *')
                    .removeClass('ui-corner-all ui-corner-top')
                    .addClass('ui-corner-bottom');

            // move the nav to the bottom
            $('.tabs-bottom .ui-tabs-nav').appendTo('.tabs-bottom');
        });
    </script>
    <style>
        /* force a height so the tabs don't jump as content height changes */
        #tabs .tabs-spacer {
            float: left;
            height: 100px;
        }

        .tabs-bottom .ui-tabs-nav {
            clear: left;
            padding: 0 .2em .2em .2em;
        }

        .tabs-bottom .ui-tabs-nav li {
            top: auto;
            bottom: 0;
            margin: 0 .2em 1px 0;
            border-bottom: auto;
            border-top: 0;
        }

        .tabs-bottom .ui-tabs-nav li.ui-tabs-active {
            margin-top: -1px;
            padding-top: 1px;
        }
    </style>
</head>
<body>

<div id='tabs' class='tabs-bottom'>
    <ul>
        <li><a href='#tabs-1'>Pitbull</a></li>
        <li><a href='#tabs-2'>Sean Kingston</a></li>
        <li><a href='#tabs-3'>Taylor Swift</a></li>
    </ul>
    <div class='tabs-spacer'></div>
    <div id='tabs-1'>
        <p>Pitbull is a Cuban–American recording artist and Latin Grammy winning rapper from Miami, Florida.</p>
    </div>
    <div id='tabs-2'>
        <p>Sean Kingston pursued a music career and debuted in 2007 with the album Sean Kingston.</p>
    </div>
    <div id='tabs-3'>
        <p>Taylor Swift, raised in Wyomissing, Pennsylvania. She moved to Nashville, Tennessee at the age of
            fourteen.</p>

        <p>She is known for her narrative songs about her personal experiences.</p>
    </div>
</div>

</body>
</html>
Try The Code

jQuery UI & Tabs: Sortable tabs

This entry is part 6 of 7 in the series jQuery UI Tabs

jQuery UI Tabs – Sortable

Drag the tabs above to re-order them.

Making tabs sortable is as simple as calling .sortable() on the .ui-tabs-nav element.

<!doctype html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>jQuery UI Tabs - Sortable</title>
    <link rel='stylesheet' href='//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <script src='//code.jquery.com/ui/1.10.4/jquery-ui.js'></script>
    <link rel='stylesheet' href='https://codecrawl.com/code/jqueryui/jqueryui_style.css'>
    <script>
        $(function () {
            var tabs = $('#tabs').tabs();
            tabs.find('.ui-tabs-nav').sortable({
                axis: 'x',
                stop: function () {
                    tabs.tabs('refresh');
                }
            });
        });
    </script>
</head>
<body>

<div id='tabs'>
    <ul>
        <li><a href='#tabs-1'>Pitbull</a></li>
        <li><a href='#tabs-2'>Sean Kingston</a></li>
        <li><a href='#tabs-3'>Taylor Swift</a></li>
    </ul>
    <div id='tabs-1'>
        <p>Pitbull is a Cuban–American recording artist and Latin Grammy winning rapper from Miami, Florida.</p>
    </div>
    <div id='tabs-2'>
        <p>Sean Kingston pursued a music career and debuted in 2007 with the album Sean Kingston.</p>
    </div>
    <div id='tabs-3'>
        <p>Taylor Swift, raised in Wyomissing, Pennsylvania. She moved to Nashville, Tennessee at the age of
            fourteen.</p>

        <p>She is known for her narrative songs about her personal experiences.</p>
    </div>
</div>

</body>
</html>
Try The Code

jQuery UI & Tabs: Tabs adding and removing

This entry is part 7 of 7 in the series jQuery UI Tabs

jQuery UI Tabs – Simple manipulation

Simple tabs adding and removing.

<!doctype html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>jQuery UI Tabs - Simple manipulation</title>
    <link rel='stylesheet' href='//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <script src='//code.jquery.com/ui/1.10.4/jquery-ui.js'></script>
    <link rel='stylesheet' href='https://codecrawl.com/code/jqueryui/jqueryui_style.css'>
    <style>
        #dialog label, #dialog input {
            display: block;
        }

        #dialog label {
            margin-top: 0.5em;
        }

        #dialog input, #dialog textarea {
            width: 95%;
        }

        #tabs {
            margin-top: 1em;
        }

        #tabs li .ui-icon-close {
            float: left;
            margin: 0.4em 0.2em 0 0;
            cursor: pointer;
        }

        #add_tab {
            cursor: pointer;
        }
    </style>
    <script>
        $(function () {
            var tabTitle = $('#tab_title'),
                    tabContent = $('#tab_content'),
                    tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
                    tabCounter = 2;

            var tabs = $('#tabs').tabs();

            // modal dialog init: custom buttons and a 'close' callback resetting the form inside
            var dialog = $('#dialog').dialog({
                autoOpen: false,
                modal: true,
                buttons: {
                    Add: function () {
                        addTab();
                        $(this).dialog('close');
                    },
                    Cancel: function () {
                        $(this).dialog('close');
                    }
                },
                close: function () {
                    form[ 0 ].reset();
                }
            });

            // addTab form: calls addTab function on submit and closes the dialog
            var form = dialog.find('form').submit(function (event) {
                addTab();
                dialog.dialog('close');
                event.preventDefault();
            });

            // actual addTab function: adds new tab using the input from the form above
            function addTab() {
                var label = tabTitle.val() || 'Tab ' + tabCounter,
                        id = 'tabs-' + tabCounter,
                        li = $(tabTemplate.replace(/#\{href\}/g, '#' + id).replace(/#\{label\}/g, label)),
                        tabContentHtml = tabContent.val() || 'Tab ' + tabCounter + ' content.';

                tabs.find('.ui-tabs-nav').append(li);
                tabs.append("<div id='" + id + "'><p>" + tabContentHtml + "</p></div>");
                tabs.tabs('refresh');
                tabCounter++;
            }

            // addTab button: just opens the dialog
            $('#add_tab')
                    .button()
                    .click(function () {
                        dialog.dialog('open');
                    });

            // close icon: removing the tab on click
            tabs.delegate('span.ui-icon-close', 'click', function () {
                var panelId = $(this).closest('li').remove().attr('aria-controls');
                $('#' + panelId).remove();
                tabs.tabs('refresh');
            });

            tabs.bind('keyup', function (event) {
                if (event.altKey && event.keyCode === $.ui.keyCode.BACKSPACE) {
                    var panelId = tabs.find('.ui-tabs-active').remove().attr('aria-controls');
                    $('#' + panelId).remove();
                    tabs.tabs('refresh');
                }
            });
        });
    </script>
</head>
<body>

<div id='dialog' title='Tab data'>
    <form>
        <fieldset class='ui-helper-reset'>
            <label for='tab_title'>Title</label>
            <input type='text' name='tab_title' id='tab_title' value='' class='ui-widget-content ui-corner-all'>
            <label for='tab_content'>Content</label>
            <textarea name='tab_content' id='tab_content' class='ui-widget-content ui-corner-all'></textarea>
        </fieldset>
    </form>
</div>

<button id='add_tab'>Add Tab</button>

<div id='tabs'>
    <ul>
        <li><a href='#tabs-1'>Pitbull</a> <span class='ui-icon ui-icon-close'
                                                role='presentation'>Remove Tab</span></li>
    </ul>
    <div id='tabs-1'>
        <p>Pitbull is a Cuban–American recording artist and Latin Grammy winning rapper from Miami, Florida.</p>
    </div>
</div>


</body>
</html>
Try The Code

jQuery UI & Tabs: Open on mouseover

This entry is part 5 of 7 in the series jQuery UI Tabs

jQuery UI Tabs – Open on mouseover

Toggle sections open/closed on mouseover with the event option.

The default value for event is “click.”

<!doctype html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>jQuery UI Tabs - Open on mouseover</title>
    <link rel='stylesheet' href='//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <script src='//code.jquery.com/ui/1.10.4/jquery-ui.js'></script>
    <link rel='stylesheet' href='https://codecrawl.com/code/jqueryui/jqueryui_style.css'>
    <script>
        $(function () {
            $('#tabs').tabs({
                event: 'mouseover'
            });
        });
    </script>
</head>
<body>

<div id='tabs'>
    <ul>
        <li><a href='#tabs-1'>Pitbull</a></li>
        <li><a href='#tabs-2'>Sean Kingston</a></li>
        <li><a href='#tabs-3'>Taylor Swift</a></li>
    </ul>
    <div id='tabs-1'>
        <p>Pitbull is a Cuban–American recording artist and Latin Grammy winning rapper from Miami, Florida.</p>
    </div>
    <div id='tabs-2'>
        <p>Sean Kingston pursued a music career and debuted in 2007 with the album Sean Kingston.</p>
    </div>
    <div id='tabs-3'>
        <p>Taylor Swift, raised in Wyomissing, Pennsylvania. She moved to Nashville, Tennessee at the age of
            fourteen.</p>

        <p>She is known for her narrative songs about her personal experiences.</p>
    </div>
</div>

</body>
</html>
Try The Code Tab Widgets Api Doc