Question

So I have this jquery code which switches tabs in a div when I click on them... I need to host the javascript on an external source, and i've called it in like this:

<script src="(link here).js?auto></script>

And i know it runs because it sets the tabs up properly, but it doesn't switch them. When I click on the tab it opens "http:// #tab1/" instead of "http:// (mywebsite).com/#tab1/"

Here is my code:

$(function () {
$('div.tabgroup').each(function () {
    var $active, $content, $links = $(this).find('a');
    $active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
    $active.addClass('active');
    $content = $($active.attr('href'));
    $links.not($active).each(function () {
        $($(this).attr('href')).hide()
    });
    $(this).on('click', 'a', function (e) {
        $active.removeClass('active');
        $content.hide();
        $active = $(this);
        $content = $($(this).attr('href'));
        $active.addClass('active');
        $content.show();
        e.preventDefault()
    })
})
});

How can I make it so it add's the current website url before the link? A live demo of it can be seen here: http://test-theme-one.tumblr.com/test

Was it helpful?

Solution

http://jsfiddle.net/mAuRG/

I applied a class to each of the content areas to simply things. HTML:

<div class="tabgroup"><a href="#home1">#home1</a></div>
<div class="tabgroup"><a href="#home2">#home2</a></div>
<div class="tabgroup"><a href="#home3">#home3</a></div>
<div class="tabgroup"><a href="#home4">#home4</a></div>
<div class="tabgroup"><a href="#home5">#home5</a></div>

<div id="home1" class="content-area">This is #home1's Content</div>
<div id="home2" class="content-area">This is #home2's Content</div>
<div id="home3" class="content-area">This is #home3's Content</div>
<div id="home4" class="content-area">This is #home4's Content</div>
<div id="home5" class="content-area">This is #home5's Content</div>

JS:

$(function () {
    var $tabs = $('div.tabgroup'),
        $links = $tabs.find('a'),
        $active = function(){
                var ret = $links.eq(0);
                $links.each(function(){
                    if ($(this).attr('href') == location.hash){
                        ret = $(this);
                    }
                });
                return ret;
            }(),
        $contentAreas = $('.content-area'),
        $activecontent = $($active.attr('href'));

    $active.addClass('active');
    $contentAreas.hide();
    $activecontent.show();

    $tabs.on('click','a',function(e){
        e.preventDefault();
        $active = $(this);
        $activecontent = $($active.attr('href'));

        $links.removeClass('active');
        $active.addClass('active');

        $contentAreas.hide();
        $activecontent.show();
    });
});

OTHER TIPS

You have several issues:

1. Tabs not switching content when clicked

Your tabs does not switch the content since they have URLs like http://.one, but not .one as you expect. So, to fix the issue you should remove http:// from the link URL. One of the ways to do it is replace

$(this).attr('href')

with

$(this).attr('href').split(/https?:\/\//).pop()

which will remove http:// and https:// from link URLs in case they do have such prefix.

2. You can't navigate to specific tab using hash

First of all expression $links.filter('[href="' + location.hash + '"]')[0] will return link with href #one for the URL http://test-theme-one.tumblr.com/test#one, that means that your links should be like <a href="#one">...</a> and not like <a href="http://.one">...</a> and your content should be <div id="one"></div>, but not <div class="one"></div>.

If you do want to use classes instead of ids for content than you should remove # from location hash (something like location.hash.substring(1)) and use links like <a href="#.one">...</a>

Conclusion

Finally your html should be:

<div class="tabgroup">
    <a href="#one"></a>
    <a href="#two"></a>
    <a href="#three"></a>
    <div class="comment">
        <div id="one">...</div>
        <div id="two">...</div>
        <div id="tree">...</div>
    </div>
</div>

and your JavaScript:

$(window.onhashchange = function () {
    var hash = location.hash || $('.tabgroup a:first').attr('href');
    $('.tabgroup a').
        removeClass('active').
        find('[href="' + hash + '"]').
        addClass('active');
    $('.tabgroup .comment>div').
        hide().
        find(hash).
        show();
});

P.S.

And by the way, you can implement tabs without JavaScript at all, just take a look at :target CSS selector. Here http://css-tricks.com/css3-tabs/ you can find how to do it :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top