Hit enter after type your search item

How to create jQuery tabs by UI plugin with 5 examples

jQuery tab plugin

In many scenarios, you have different pieces of information related to a specific topic while it is important to present all information on one web page. For example, you might have seen in e-commerce sites product pages, that information is presented in Tabs format for features, reviews etc.

The tab feature is quite useful in that it allows you to cater more information while occupying less space, however, you must take into account the SEO perspective in view while managing tabs in your web pages.

A Tab example

The tabs can be filled by the static content or you can use the dynamic or AJAX driven content as well. There are different ways to create tabs in your web pages e.g. bootstrap, jQuery etc. however, this tutorial will deal only creating the tabs by using jQuery tab plug-in.

The jQuery, just like many other features like dialog, sliders, auto-complete etc. provides the way to create tabs by using its plugin. Creating jQuery UI tab is pretty simple that it needs only a few line of codes.

Following is the simple process to create jQuery tabs:

  • Include the jQuery library.
  • You have to include jquery UI library in web document as well.
  • CSS or style can be default as provided by jQuery or you can use as per the design of your website.
  • jQuery UI tab uses order or unordered list element i.e <ol> and <ul>.
  • The title of each tab is defined inside <li> tag of ordered or unordered lists that are wrapped by anchor tag <a>.
  • Finally, you can use div, paragraph or another tag to present Tab’s content but it must contain an ID attribute that corresponds to ID or hash of <a> tag, that contains the title (as mentioned in the last point).

Now, let us look at a few examples of using jquery tab UI plug-in.

A simple Tab example in jQuery UI

In the following example, we have created simple tabs by using Tab UI plugin in jQuery. The demo shows four tabs with one line of content. For this example, we have changed the default colors by using in-line CSS.

jquery tab basic

See online demo and code

The Markup used in this example:

 The simple tab jQuery code:


As you can see in the code, first of all, a div element with ID=tab_example is created. Within this, an unordered list <ul> with four list elements are created that represents titles of tabs. Each of these tabs contains the <a> tag with an ID. Keep this ID in mind as this will correspond in the lower div that contains the content for each tab.

Lastly, we created four div elements with same ids as we used for <a> IDs in list items.

Tab example that opens on mouse-over

In the above example, you need to click on a tab in order to open. By simply using event option with ‘mouseover’ value you can open tabs as mouse hovers over a tab. Following is a working demo of opening a tab as the mouse is over a tab.

jquery tab mouseover

See online demo and code

Not only you can use mouseover value but also other valid values related to events like mouseenter, mouseleave, dblclick etc.

If you use the dblclick value in the event option, you can open tabs by using double click. Upon the single click, the tabs will not be opened in that case.

See online demo and code

The jQuery code:

 The CSS:

In the above two examples, we overridden CSS classes provided by jQuery-UI to design the theme of Tabs. You can change it as per the need. You may get the default styles here: http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css

An example of jQuery Tabs with close option

In this example, we have created tabs with a close option. Unfortunately, we don’t have simple option provided by jQuery UI to make it closeable by adding an option. You have to handle it by a little programming. Before explaining how we added the close icon in tabs, first look at its graphic and run-able demo:

jquery tab close

See online demo and code

The code of jQuery with close option:


First of all, how close icon is added? It is simply by using a span element with icon class:

<span class=’ui-icon ui-icon-close’ role=’presentation’></span>

i.e. ui-icon-close class.

Then in the script section, we used the jQuery on method where we dealt with the closing of tabs.

A vertical Tabs example

Until now we have worked with horizontal tabs that you can create quite easily and also used some options and event provided by jQuery tabs plugin. In this example, we will create vertical tabs with custom CSS.

Though there is no simple and straightforward “orientation” option to show tabs as vertical like in the case of the vertical slider that we created, yet creating vertical tabs is easy.


jquery tab vertcal

See online demo and code

The vertical tab code:

The CSS:

You can see in the demo and code that it used a few classes of jQuery UI plugin to create a vertical tab example. You can style it according to your theme and amend colors and width etc. Just like in the horizontal tabs demo you can use the event: “mouseover” to display tab’s content as the mouse is over a particular tab. Otherwise, just remove that line of code from demo code to make it clickable to open a tab.

A tab example with AJAX data

Not only you can present the static content in tabs of jQuery UI but you can also use DB driven or information from the text or other files by using AJAX call. This is quite easy to accomplish in jQuery tabs. All you need is to specify text/PHP etc. file in tabs and use the built-in AJAX call to retrieve and present data in the Tabs.

Following example shows how you can do it easily.

jquery tab ajax

See online demo and code

The jQuery code:

 The Markup section calls the text files:

In the above demo, we used text files to populate tab’s content with AJAX. You may use the script files like PHP to get database’s data. You can use events like beforeLoad to manage error handling if AJAX call has some issues.

The beforeLoad event of tab widget occurs just before AJAX request is made. You can change ui.jqXHR e.g. ui.jqXHR.error to do error handling .e.g. Database connection could not be established or data was unable to load or some other message.

This div height required for enabling the sticky sidebar