views:

47

answers:

4

I have a nested unordered list representing a tree hierarchy. There can be many deeply nested ul tags in the unordered list. Very simple example:

<ul>
    <li><a href="#" class="allowed">Link</a>
        <ul>
            <li><a href="#" class="allowed">Link</a></li>
            <li><a href="#" class="allowed">Link</a></li>
        </ul>
    </li>
    <li><a href="#" class="allowed">Link</a>
        <ul>
            <li><a href="#" class="disallowed">Link</a></li>
            <li><a href="#" class="disallowed">Link</a></li>
        </ul>
    </li>
</ul>

As you can see, some links can have class "allowed". When a link like that is clicked, I would like to get a next a tag in the tree and if it has class "disallowed", change it to "allowed".

How can I get the next a tag in the tree?

UPDATE:

What I mean. Before:

<ul>
    <li><a href="#" class="allowed">Link</a>
        <ul>
            <li><a href="#" class="allowed">Link</a></li>
            <li><a href="#" class="allowed">Link</a></li>
        </ul>
    </li>
    <li><a href="#" class="allowed">Link</a><!-- this gets clicked on -->
        <ul>
            <li><a href="#" class="disallowed">Link</a></li>
            <li><a href="#" class="disallowed">Link</a></li>
        </ul>
    </li>
</ul>

HTML changes to this:

<ul>
    <li><a href="#" class="allowed">Link</a>
        <ul>
            <li><a href="#" class="allowed">Link</a></li>
            <li><a href="#" class="allowed">Link</a></li>
        </ul>
    </li>
    <li><a href="#" class="allowed">Link</a>
        <ul>
            <li><a href="#" class="allowed">Link</a></li>
            <li><a href="#" class="disallowed">Link</a></li>
        </ul>
    </li>
</ul>

And so on.

A: 

you could do something like

$('a.allowed').click(function(){
    $(this).next().find('a:first').attr('class', 'allowed');
});

this will make all the a tags under the ul next to the a link that was clicked have a class allowed.

rob waminal
Yeah but that doesn't have to be case. The above HTML structure is just an example, There could be another nested ul inside.
Richard Knop
so you want all `a` under the link that is clicked should turn to `allowed`? in your edit, the only link that is changed is just the first `a` that is found.
rob waminal
you could try my edit..
rob waminal
+1  A: 
$(document).ready(function() {
      var el = null;
      $('ul li a.allowed').click(function(){
        el = $(this);
        while($(el).closest('.ul')){
          el = $(el).closest('.ul');
          var a = $(el).find('li a.disallowed:first');
          if(a){
            $(a).removeclass('disallowed').addClass('allowed');
            return true;
          }
        }
    });
 });
Mithun P
+1  A: 
$('.allowed').live(function() {
    var links= $('a');
    var ix= links.index(this);
    if (ix!==-1 && ix<links.length-1)
        links.eq(ix+1).removeClass('disallowed').addClass('allowed');
});
  • this uses live() to catch clicks, assuming that after after a link is changed to class="allowed", clicking that should perform the same action

  • it looks for the next link on the whole page. If you only want to check inside a particular container, use that as the parent to find links in, eg. var links= $('#myul a');, for efficiency and to prevent it affecting other page links

bobince
Thanks, I will go try it, If it works, I will accept your answer.
Richard Knop
+1  A: 
$(function(){
    $('ul li a').click(function(){
        if ($(this).is('.allowed')) {
            var nextElem = $(this).next('ul').length?$(this).next('ul'):$(this).closest('li').next('li');
            nextElem.find('a:first').
                filter(function(){
                    return $(this).is('.disallowed');
                }).
                removeClass('disallowed').
                addClass('allowed');
        }
        return false;
    });
});

Phew! demo

Reigel
It doesn't work. Try clicking on the 11th link from the top.
Richard Knop
excuse me?... 11th is disallowed right? so that will have no effect..
Reigel
Yes but click links after 11th is allowed. Then click the 11th link and see what happens (more than one link after the 11th become allowed).
Richard Knop
okay, I don't get you. 11th and 12th not allowed right? I click 10th, 11th become allowed. I click 11th, 12th become allowed. But if I click 11th without clicking 10th first, 12th will not be allowed. Isn't that what you want?
Reigel
If 11th is allowed and I click it, next three links become allowed.
Richard Knop
hmmm you're not really clear on everything... I hope you got it working now...
Reigel
I am writing in literary English and all my sentences are completely clear and comprehensible. I don't understand what you have problem with. Others got what I meant :P
Richard Knop