Skip to content
","url":"https://ez4code.com/snippets/jquery-ui-tabs","keywords":"tabs, widget, ui","author":{"@type":"Person","name":"EZ4Code Team"},"publisher":{"@type":"Organization","name":"EZ4Code","logo":{"@type":"ImageObject","url":"https://ez4code.com/logo.png"}},"datePublished":"2024-01-01","dateModified":"2026-08-01","image":"https://ez4code.com/og-image.png"}
jQuery UI

Tabs

Tabbed content panels with AJAX loading and event-driven switching.

By EZ4Code Team
tabswidgetui

Code

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">First</a></li>
    <li><a href="#tabs-2">Second</a></li>
    <li><a href="ajax-content.html">AJAX Tab</a></li>
  </ul>
  <div id="tabs-1"><p>Static content 1.</p></div>
  <div id="tabs-2"><p>Static content 2.</p></div>
</div>

<script>
$("#tabs").tabs({
  active: 0,
  collapsible: false,
  event: "click",
  heightStyle: "auto",
  beforeLoad: function(event, ui) {
    ui.jqXHR.fail(function() {
      ui.panel.html("Failed to load content.");
    });
  },
  activate: function(event, ui) {
    console.log("Switched to tab:", ui.newTab.index());
  }
});

// Programmatic tab switch
$("#tabs").tabs("option", "active", 2);
</script>

Explanation

Builds tabbed panels from an unordered list of anchors paired with content divs; anchors pointing to URLs load content via AJAX. The beforeLoad hook gives access to the jqXHR for error handling, and activate fires after every switch. Use the active option by index to programmatically change tabs.

More jQuery UI Snippets