Jquery tabs


With the following code, how can I select which tab to open using hyper links?

// Easy Responsive Tabs Plugin
// Author: Samson.Onna <Email : samson3d@gmail.com>
(function ($) {
        easyResponsiveTabs: function (options) {
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                type: 'default', //default, vertical, accordion;
                width: 'auto',
                fit: true
            var options = $.extend(defaults, options);
            var opt = options, jtype = opt.type, jfit = opt.fit, jwidth = opt.width, vtabs = 'vertical', accord = 'accordion';

            //Main function
            this.each(function () {
                var $respTabs = $(this);
                $respTabs.find('ul.resp-tabs-list li').addClass('resp-tab-item');
                    'display': 'block',
                    'width': jwidth

                $respTabs.find('.resp-tabs-container > div').addClass('resp-tab-content');
                //Properties Function
                function jtab_options() {
                    if (jtype == vtabs) {
                    if (jfit == true) {
                        $respTabs.css({ width: '100%', margin: '0px' });
                    if (jtype == accord) {
                        $respTabs.find('.resp-tabs-list').css('display', 'none');

                //Assigning the h2 markup to accordion title
                var $tabItemh2;
                $respTabs.find('.resp-tab-content').before("<h2 class='resp-accordion' role='tab'><span class='resp-arrow'></span></h2>");

                var itemCount = 0;
                $respTabs.find('.resp-accordion').each(function () {
                    $tabItemh2 = $(this);
                    var innertext = $respTabs.find('.resp-tab-item:eq(' + itemCount + ')').html();
                    $respTabs.find('.resp-accordion:eq(' + itemCount + ')').append(innertext);
                    $tabItemh2.attr('aria-controls', 'tab_item-' + (itemCount));

                //Assigning the 'aria-controls' to Tab items
                var count = 0,
                $respTabs.find('.resp-tab-item').each(function () {
                    $tabItem = $(this);
                    $tabItem.attr('aria-controls', 'tab_item-' + (count));
                    $tabItem.attr('role', 'tab');

                    //First active tab
                    $respTabs.find('.resp-tab-content').first().addClass('resp-tab-content-active').attr('style', 'display:block');

                    //Assigning the 'aria-labelledby' attr to tab-content
                    var tabcount = 0;
                    $respTabs.find('.resp-tab-content').each(function () {
                        $tabContent = $(this);
                        $tabContent.attr('aria-labelledby', 'tab_item-' + (tabcount));

                //Tab Click action function
                $respTabs.find("[role=tab]").each(function () {
                    var $currentTab = $(this);
                    $currentTab.click(function () {

                        var $tabAria = $currentTab.attr('aria-controls');

                        if ($currentTab.hasClass('resp-accordion') && $currentTab.hasClass('resp-tab-active')) {
                            $respTabs.find('.resp-tab-content-active').slideUp('', function () { $(this).addClass('resp-accordion-closed'); });
                            return false;
                        if (!$currentTab.hasClass('resp-tab-active') && $currentTab.hasClass('resp-accordion')) {
                            $respTabs.find('.resp-tab-content-active').slideUp().removeClass('resp-tab-content-active resp-accordion-closed');
                            $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active');

                            $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + ']').slideDown().addClass('resp-tab-content-active');
                        } else {
                            $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active');
                            $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + ']').addClass('resp-tab-content-active').attr('style', 'display:block');
                    //Window resize function
                    $(window).resize(function () {



Do you mean which tab should display when the page opens?

By default it opens to the first tab…

I want to use a link to make a specific tab open… I guess something like –> www.domainname.com/page.html#tabid

Could you post a link to a page where I could see this in action?


Not a lot of info on how to further advance the script… ie linking to specific tabs across the url…

The currently active tab has the class of “resp-tab-active”, you can use this to create be able to link to a specific tab.

First add links to the tab handles:

<ul class="resp-tabs-list">
  <li><a href="#tab1">Responsive Tab-1</a></li>
  <li><a href="#tab2">Responsive Tab-2</a></li>
  <li><a href="#tab3">Responsive Tab-3</a></li>

Then we need to get the hash from the url and apply the correct class:

var hash = window.location.hash,
    lis = $("ul.resp-tabs-list > li");
$("a[href='" + hash + "']").addClass("resp-tab-active");

I made a demo page to show it working.

Link to tab one
Link to tab two
Link to tab three

I hope that helps you.

Thinking about it, you should probably check for the existence of a hash first, before removing any classes.
That way, you can still link to index.html (as opposedto index.html#tab1)

e.g. (untested)

var hash = window.location.hash,
    lis = $("ul.resp-tabs-list > li");

if (hash){
  $("a[href='" + hash + "']").addClass("resp-tab-active");

Think I have a handle in it…

A couple of glitches… The first tab doesn’t load as default if no handle is sent across the url and <a href=“./index.html#tab2”>OUR STAFF</a> doesn’t reload page at all if clicked while on the index page, its seems like it has broken the css now too.


See my comment above.

Can you post a link to your page?


I see what you mean - basically that the url doesn’t change from http://www.yoursite.co.nz/Navigation/ to http://www.yoursite.co.nz/Navigation/#tab2
This only seems to happen intermittently, which is a bit weird and will be something to do with how the plugin handles the clicks.

If this is important to you, then change your code thus:

  type: 'default',        
  width: 'auto',
  fit: true 

var hash = window.location.hash,
    lis = $("ul.resp-tabs-list > li"),
    anchors = $("ul.resp-tabs-list").find("a");

$("a[href='" + hash + "']").addClass("resp-tab-active");

  window.location.hash = $(this).attr("href");

You’ll have to adapt the CSS to suit (now that you have added anchor tags to the tab handles)


li.resp-tab-item > a { text-decoration: none; }


I cant seem to sort it with the code above… Where should I be placing that code? In the easyResponsiveTabs.js or replace script at footer of the index.html file?



The latter.

Ok, that’s where I have it, but it appears to be reacting in the same manor…

Its really odd, when you access it via a link, the tab is correctly highlighted, but the wrong info displays in the tab. When you click the tap, it shifts to the left, the style of the text changes and the correct info displays…

This script is taking to much time to make work, I have tried another version: http://os.alfajango.com/easytabs/

Seems to work, just need to use css to match the look of original version…

Seems to be working great… Next thing is to try and the menus that popup over the images to have the correct menu displaying dependant on which page is open…

Would that be possible?


Hi there,

Good news :slight_smile:

I would guess so.
Try $(yourSelector).trigger("mouseover")

Hmm haven’t really got a handle on it yet… The js code for image menu is:

		$(function() {
                /* position of the <li> that is currently shown */
                var current = 0;
				var loaded  = 0;
				for(var i = 1; i <4; ++i)
					$('<img />').load(function(){
						if(loaded == 3){
								var $this = $(this);
								/* if we hover the current one, then don't do anything */
								if($this.parent().index() == current)

								/* item is bg1 or bg2 or bg3, depending where we are hovering */
								var item = e.target.id;

								this is the sub menu overlay. Let's hide the current one
								if we hover the first <li> or if we come from the last one,
								then the overlay should move left -> right,
								otherwise right->left
								if(item == 'bg1' || current == 2)
									$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-266px 0)"},300,function(){
									$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(266px 0)"},300,function(){

								if(item == 'bg1' || current == 2){
									/* if we hover the first <li> or if we come from the last one, then the images should move left -> right */
									$('#menu > li').animate({backgroundPosition:"(-800px 0)"},0).removeClass('bg1 bg2 bg3').addClass(item);
									/* if we hover the first <li> or if we come from the last one, then the images should move right -> left */
									$('#menu > li').animate({backgroundPosition:"(800px 0)"},0).removeClass('bg1 bg2 bg3').addClass(item);

								We want that if we go from the first one to the last one (without hovering the middle one),
								or from the last one to the first one, the middle menu's overlay should also slide, either
								from left to right or right to left.
								if(current == 2 && item == 'bg1'){
									$('#menu .sub'+parseInt(current)).stop().animate({backgroundPosition:"(-266px 0)"},300);
								if(current == 0 && item == 'bg3'){
									$('#menu .sub'+parseInt(current+2)).stop().animate({backgroundPosition:"(266px 0)"},300);

								/* change the current element */
								current = $this.parent().index();
								/* let's make the overlay of the current one appear */
								$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},300,function(){
					}).attr('src', 'images/'+i+'.jpg');
                dir:1 - move left->right
                dir:0 - move right->left
                function move(dir,item){
                        $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},200);
                        $('#bg2').parent().stop().animate({backgroundPosition:"(-266px 0)"},300);
                        $('#bg3').parent().stop().animate({backgroundPosition:"(-532px 0)"},400,function(){
                            $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
                        $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},400,function(){
                            $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
                        $('#bg2').parent().stop().animate({backgroundPosition:"(-266px 0)"},300);
                        $('#bg3').parent().stop().animate({backgroundPosition:"(-532px 0)"},200);

Hi there,

Sorry for the delay in getting back to you.

Place this code just before the closing body tag of your page and you will see what I am trying to demonstrate:


To trigger the effect, you will then have to click on the “Our Staff” tab when one of the menus other than “Student Info” is showing.