views:

148

answers:

3

Why $('a.current').parent('li').addClass('current'); and $(this).hasClass('current').parent('li').addClass('current'); are not working?

a click event must add li.current http://jsfiddle.net/laukstein/ytnw9/

Update: Dropbox is Down, so I updated http://jsfiddle.net/laukstein/ytnw9/1/ with full JS

$(function(){
 var list=$('#list'),
     elementsPerRow=-1,
     loop=true,
     // find first image y-offset to find the number of images per row
     topOffset=list.find('a:eq(0)').offset().top,
     numTabs=list.find('li').length-1,
     current,newCurrent;

 function changeTab(diff){
  // a.current set by jQuery Tools tab plugin
  current=list.find('a.current').parent('li').addClass('current').index();
  newCurrent=(loop)?(current+diff+numTabs+1)%(numTabs+1):current+diff;
 if(loop){
   if(newCurrent>numTabs){newCurrent=0;}
   if(newCurrent<0){newCurrent=numTabs;}
 }else{
   if(newCurrent>numTabs){newCurrent=numTabs;}
   if(newCurrent<0){newCurrent=0;}
 }
  // don't trigger change if tab hasn't changed (for non-looping mode)
 if (current!=newCurrent){
   list.find('li').eq(current).removeClass('current');
   list.find('li').eq(newCurrent).addClass('current').find('a').trigger('click'); // trigger click on tab
 }
 }
 list
  // set up tabs
  .tabs("#content",{effect:'ajax',history:true})
  // find number of images on first row
  .find('a').each(function(i){
      // $(this).hasClass('current').parent('li').addClass('current');
      if(elementsPerRow<0&&$(this).offset().top>topOffset){
         elementsPerRow=i;
      }
  });
  // Set up arrow keys
  // Set to document for demo, probably better to use #list in the final version.
 $(document).bind('keyup focus',function(e){
    var key=e.keyCode;
    if(key>36&&key<41){
      if(key==37){changeTab(-1);}              // Left
      if(key==38){changeTab(-elementsPerRow);} // Up
      if(key==39){changeTab(+1);}              // Right
      if(key==40){changeTab(+elementsPerRow);} // Down
      e.preventDefault();
    }
 });
 // toggle looping through tabs
 $(':button').click(function(){
   loop=!loop;
   $('#loopStatus').text(loop);
 });
 $('a.current').parent('li').addClass('current');
});​

EDIT: Adding HTML from jsFiddle link that was omitted from question

<button>Loop</button> <span id="loopStatus">true</span><br />
<ul id="list">
    <li><a class="current" href="http://dl.dropbox.com/u/6594481/tabs/one.html" title="one">1</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/two.html" title="two">2</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/three.html" title="three">3</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/four.html" title="four">4</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/five.html" title="five">5</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/six.html" title="six">6</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/seven.html" title="seven">7</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/eight.html" title="eight">8</a></li>
    <li><a href="http://dl.dropbox.com/u/6594481/tabs/nine.html" title="nine">9</a></li>
</ul>
<div id="content" style="clear:both;">Loading...</div>​
+4  A: 
SLaks
`.filter` does not solve the problem at all.
Binyamin
+2  A: 

$(this).hasClass('current').parent('li').addClass('current'); certainly won't work, because hasClass returns a boolean value (true/false) not the jQuery object. You can't continue chaining after you have called it.

You can use filter instead:

$(this).filter('.current').parent('li').addClass('current');
lonesomeday
`$('a').filter('.current').parent('li').addClass('current');` is not working either
Binyamin
What exactly isn't working? It's hard to see without a complete, working example... The jsfiddle isn't working, partly because the tabs.js and toolbox.history.js links aren't working.
lonesomeday
Dropbox seems Down, so I updated http://jsfiddle.net/laukstein/ytnw9/1/ with full JS
Binyamin
+1  A: 

You only seem to be using that bit of code in the changeTab function.

This only seems to be called when using the cursor keys.

If I go to your demo and use cursor keys the orange highlight moves around. If I click it doesn't but there isn't any code that picks up clicks that I can see...

Edit to add: (converted from comment and realised it was actually an extension to my answer)

After looking at the tabs code that you pointed me at I'm still not sure it is calling the code in the changeTab function.

If I change the tabs declaration to

.tabs("#content",{effect:'ajax',history:true, onClick:function(){changeTab(0)}})  

then it will highlight the clicked cell but of course won't deselect the old one because the current index has already changed.

I've put a crude fix in for that that can be seen at jsfiddle.net/bhvYM (a fork). That involves clearing all li.current classes before setting the new one. Its a bit hacked so may not be doing everything exactly as you want (I didn't look closely at the current/newCurrent code) but hopefully its a start that will help you get it working to where you want.

My thought is that this now at least puts the li class on which I think is the thrust of your question. If it is not then I have totally missed the point here. :)

Chris
Take a look in Resources tabs.js
Binyamin
Ah, I'm being a muppet. Though currently that seems to be linking to a page on dropbox that is returning a 500 error. Still, its interesting to note that when it was working a bit better it was doing the right thing for the cursor keys and not for the clicking. I can't see the JS for the tabs script but is it definitely set to call that function when you click on one of the boxes? There's no possibility of a typo or needing to set up the additional eventhandler or anything like that (probably silly questions but I can't check them for myself).
Chris
Check http://jsfiddle.net/laukstein/ytnw9/1/
Binyamin
Even after looking at that I'm not sure it is calling the code in the changeTab function. If I change the tabs declaration to `.tabs("#content",{effect:'ajax',history:true, onClick:function(){changeTab(0)}})` then it will highlight the clicked cell but of course won't deselect the old one because the current index has already changed. I've put a crude fix in for that that can be seen at http://jsfiddle.net/bhvYM/ (a fork). Its a bit hacked so may not be doing everything perfectly but hopefully its a start that will help you get it 100% working. Either that or I've missed the point here. :)
Chris
Thanks! Why http://jsfiddle.net/bhvYM/ click event `addClass()` works very slow?
Binyamin
When I click on things it seems to run very fast. I do also get a javascript error to do with the page request (proxy authentication required). It is possible that it is waiting for the page load to happen before it does your custom click. You could try doing it with an onbeforeclick to see if that responds faster but in this case I don't think it has changed the current so your orange highlight is lagging one click behind where it should be...
Chris
The other option is having a totally separate function for the highlights on clicking since you don't need all the selection changing stuff. http://jsfiddle.net/bhvYM/1/ has a simple click function on the LI elements that recolours them.
Chris
Thank you again! It works almost excellent. `$('a').filter('.current').parent('li').addClass('current');` does not work when I remove class="current" from HTML http://jsfiddle.net/laukstein/ytnw9/3/ (you can see in Firebug there `a` has class `current` and **`li` has not**. I hope you have a solution for this final thing too!
Binyamin
Could you update http://jsfiddle.net/laukstein/ytnw9/3/ too? (you can see in Firebug `a` has class `current` and **`li` has not** even with `$('a').filter('.current').parent('li').addClass('current');`.
Binyamin
Is it just the initial state you are talkign about? If so I'm not quite sure what is going on but setting `"initialIndex:0"` in the tabs config seems to get it to highlight properly for me.
Chris
Here you can see that it does not work so - http://dl.dropbox.com/u/6594481/tabs-2010/demo1.html#five.html (`$('a.current').removeClass('current');` does not work), http://dl.dropbox.com/u/6594481/tabs-2010/demo2.html#five.html (Firebug Console shows that is has `2 GET request` because of `initialIndex:0` =( and `$('a').filter('.current').parent('li').addClass('current');` does not work). Hope you can support the issue for that!
Binyamin
Follow the JS and look at what it does... On setup it looks at the initialIndex and runs as if you've clicked on it. If the initialIndex is not the same as the current index then it will do the load. If it is already the current then it will leave the function. I've not tested but I assume if you set current correctly (ie to the same as active tab) then it will perform the click, the ajax won't run but your click handler will. I've not tested this though, you can do that yourself (sorry, spent quite a lot of time here already so leaving you more to yourself now).
Chris
This http://dl.dropbox.com/u/6594481/tabs-2010/demo1.html#five.html is problematic with `$('a.current').removeClass('current');`. I do not know why it does not remove class.
Binyamin