Hi There,

I have found a very usefull and lightweight code that is made by the famous Tim Van Damme, a Dutch compatriot.

Here is the code, i will explain my question beneath.

The js that works in combination with the js libary.

<script language="javascript" type="text/javascript"> 


jQuery(document).ready(function($) {

  $('.tabs').each(function () {
	var $links = $(this).find('a'),

      panelIds = $links.map(function() { return this.hash; }).get().join(","),
      $panels = $(panelIds),
      $panelwrapper = $panels.filter(':first').parent(),
      delay = 500,
      heightOffset = 10; // we could add margin-top + margin-bottom + padding-top + padding-bottom of $panelwrapper
      
    $panels.hide();
    
    $links.hover(function () {
      var link = this, 
        $link = $(this);
      
      // ignore if already visible
      if ($link.is('.tabs-selected')) {
        return false;
      }
      
      $links.removeClass('tabs-selected');
      $link.addClass('tabs-selected');
      
      document.title = 'jQuery look: Tim Van Damme - ' + $link.text();
              
      if ($.support.opacity) {
        $panels.stop().animate({opacity: 0 }, delay);
      }
      
      $panelwrapper.stop().animate({
        height: 0
      }, delay, function () {
        var height = $panels.hide().filter(link.hash).css('opacity', 1).show().height() + heightOffset;
        
        $panelwrapper.animate({
          height: height
        }, delay);
      });
    });
    
    $links.filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click();

  });
  });

</script>

The corresponding HTML code

<ul class="tabs tabs-nav">
<li><a href="#news"><span>News</span></a></li>
<li><a href="#blog"><span>Blog</span></a></li>
</ul>    
   
 <div class="panels">

     <div id="news" class="panel">
      <h1> hello news panel</h2>
      </div>

     <div id="blog" class="panel">
      <h1> hello blog panel</h2>
      </div>

</div>

This all works fine and I just want 1 small modification which i can't get done.

I want this list item

<li><a href="#news"><span>News</span></a></li>

like this so i can use the href to actually link to a page

<li><a href="http:www.mysite.nl/news" rel="#news"><span>News</span></a></li>

I am not that familiar with js but i tryed multiple things and searched a while on the internet but can't get the job done.

I thought this would be simple to change, well, not for me right. Can one of you point me in the right direction?

Dani AI

Generated

Good thread — nailed two practical ways to keep a real link while still targeting an on‑page panel. Below are a few clarifying points and a slightly safer, more future‑friendly pattern you can apply.

Prefer a data attribute instead of repurposing rel. Using data-target keeps your markup semantic and avoids colliding with rel semantics. Leave the href pointing at the real page (so the link still works without JavaScript) and let the script read data-target first, falling back to the hash when needed. Example HTML (illustrative only):

<li><a href="https://example.com/news" data-target="#news">News</a></li>

When wiring the behavior in JavaScript, bind activation to click and keyboard focus (not just hover) so tabs work on touch and for keyboard users. Intercept the click only when the user intends to stay on the page: call preventDefault() for plain clicks, but let modified clicks (Ctrl/Cmd or middle‑click) and keyboard navigation open the link normally. After showing the panel, update location.hash (or use history.pushState) so the selection is bookmarkable and back/forward works.

A few practical cautions:

  • Hover‑only activation breaks on touch devices and for keyboard users — add focus/click handlers or use progressive enhancement so the link still navigates without JS.
  • Use data-* attributes (access via .data('target') or .attr('data-target')) for reliability across browsers instead of overloading rel.
  • Maintain accessibility: add role="tablist"/role="tab"/role="tabpanel", set aria-selected on the active link, and move focus appropriately when panels change.

In short: ’s fixes are solid; prefer data-target + click/focus handling + careful preventDefault() logic to get a robust, accessible solution that still preserves real links for non‑JS users.

Recommended Answers

All 7 Replies

Frankey,

Change

var panelIds = $links.map(function(){ return this.hash; }).get().join(","),
//to
var panelIds = $links.map(function(){ return (this.getAttribute('rel')) ? this.getAttribute('rel') : this.hash; }).get().join(","),

and

var height = $panels.hide().filter(link.hash).css('opacity',1).show().height() + heightOffset;
//to
var panelId = (link.getAttribute('rel')) ? link.getAttribute('rel') : link.hash;
var height = $panels.hide().filter(panelId).css('opacity',1).show().height() + heightOffset;

I only tested in IE. If it doesn't work in FF, Opera etc. then try replacing link.getAttribute('rel') with link.rel (in four places).

Airshow

Airshow, many thanks for solving this. Great you had some sparetime to look into my code.

It works properly in IE 8 and FF 3.6, don't tested it yet in other browsers buti assume it will do fine there.

I only got rid of the VAR before the PANELIDS cause that wasn't in the original code actually.

Again Airshow, wonderfull that people putting effort to help others. We should put that in al aspects of life don't we?

Only too pleased to be of some help Frankey.

I actually made an error by inserting var . Those lines of code are separared by , , so the leading var covers everything up to heightOffset = 10; .

For lines of more than a few characters, I would personally choose to make separate statements each starting with var and ending in ; . In fact that's what I did to test out the changes. I remembered to replace the ; with a , but I forgot to remove var in my reply.

Airshow

I have just realised, there's an easier way (with a constraint).

Simply use your unmodified original code with :

<li><a href="http:www.mysite.nl/news#news"><span>News</span></a></li>

This is a rather odd formulation in that the href's "http:www.mysite.nl/news" will be interpreted by the server (onclick) via HTTP, while its hash string "#news" will be interpreted by javascript (onmouseover). Odd but sort of clever and it's possible that this is what your compatriot, Mr Van Damme, had in mind.

The constraint (as I'm sure you can see) is that the href can't have an independent hash string of its own.

Airshow

I used your first suggestion, i works quiet good so i will stick to that, unless you strongly advice me to use the other.:-/

I thnk that both versions should work Frankey.

If you have tested the first version cross-browser then by all means use it.

Airshow

never too late, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.