views:

755

answers:

5

Hi,

The following lines of code do two things. The first two lines add classes to the siblings immediately before and after an element with a given id (in this case a UL with id "nav")

The last two lines add the classes first and last to the first and last sibling elements whose parent has the id "nav".

This is jquery. How would I achieve the same with Mootools 1.11. Any help appreciated.

$('#nav .active').prev().addClass('prevtab');
$('#nav .active').next().addClass('nexttab');
$('#nav').children(":first").addClass('first');
$('#nav').children(":last").addClass('last');
+3  A: 

Give this a shot.

$('nav').getNext().addClass('nexttab');
$('nav').getPrevious().addClass('prevtab');
var c = $('nav').getChildren();
c[0].addClass('first');
c[c.length-1].addClass('last');

Edit: Working now.

Second/Third edits:

Ok, I misunderstood the way you're using the .active class. Try this:

var n = $('nav').getElement('.active').getNext();
if (n) n.addClass('nexttab');
var p = $('nav').getElement('.active').getPrevious();
if (p) p.addClass('prevtab');
var c = $('nav').getChildren();
c[0].addClass('first');
c[c.length-1].addClass('last');
zombat
Very close now, thank you zombat.Within the ul#nav element, everything works perfectly when elements other then the first and last li are active.If the first or last li's are active, the none of the classes(nexttab,prevtab, first,last) are added at all.strange :)
Ah, yeah, there was a JS error if there was no prev/next element, so the whole script would stop. I gave it another edit, see how that goes. Sorry for the weak JS-fu.
zombat
A: 
$each($$('#nav .active'),function(item){
                            item.getPrevious().classname='prevtab';
                         }
      );

This is the first line.

Second you can use getNext
Third you can use $('#nav').getFirst //make sure it has children....
Fourth you can use getLast

Itay Moav
A: 

Thank you very much for the help guys.

Unfortunately neither answers seems to work. Not sure if it makes any difference, but I need to use mootools 1.1

greencoconut
Ah, yes. There's a big difference between MooTools 1.1 and 1.2. 1.2 isn't backwards compatible. I'll try to come up with a 1.1 answer for you.
zombat
In that case my apologies to all responders for not mentioning the mootools version in the initial question. I really appreciate your help with this.
greencoconut
No problems. Just put an edit on your question and mention it. Which particular sub-version of MooTools 1.1 are you using? The code in my original post actually still works for me when I use 1.11 from Google's AJAX Library. Maybe you could post a snippet of the HTML source from your document?
zombat
Using mootools 1.1There is no edit button available to me, and I can't comment as I don't have enough rep.Your solution is doing something, but not targeting the correct element.This is the result I want and am getting with jquery:<ul id="nav"><li class="first"></li><li></li><li class="prevtab"></li><li class="active"></li><li class="nexttab"></li><li class="last"></li></ul>This is the result of your js:<div class="prevtab"></div><ul id="nav"><li></li><li></li><li></li><li class="active"></li><li></li><li></li></ul><div class="nexttab"><div>
greencoconut
Ok, I edited my post above, try that.
zombat
A: 

formatting doesn't work in comments so repeating for zombat:

Using mootools 1.1 There is no edit button available to me, and I can't comment as I don't have enough rep.

Your solution is doing something, but not targeting the correct element.

This is the result I want and am getting with jquery:

<ul id="nav">
<li class="first"></li>
<li></li>
<li class="prevtab"></li>
<li class="active"></li>
<li class="nexttab"></li>
<li class="last"></li>
</ul>

This is the result of your js:

<div class="prevtab"></div>
<ul id="nav">
<li></li>
<li></li>
<li></li>
<li class="active"></li>
<li></li>
<li></li>
</ul>
<div class="nexttab"><div>
greencoconut
A: 

No other way to comment (terrible UX), so thanking as an answer.

Thank you very very much zombat. I really appreciate your assistance with this. Your final edit worked perfectly.I'll be back later to select your answer.

Again, thank you very much. Appreciate you taking the time to help.

Cheers

greencoconut