views:

69

answers:

3

I have html list

<ol id="newlist">
        <li>Test
            <ol>
                <li>1</li>
                <li>2</li>
                <li>3</li>
            </ol>
        </li>
        <li>Another test
            <ol>
                <li>1</li>
            </ol>
        </li>
        <li>Cool Test
            <ol>
                <li>1</li>
                <li>2</li>
            </ol>
        </li>
    </ol>

Now i have hidden the list using the css...

        #newlist li {
            display:none;
            list-style: none;
        }

I want to display the list and the only the descendants which have greater than 1 descendants...

the output should be...

Test
   1
   2
   3
Another test
Cool Test
   1
   2

I have used jquery and able to get the output...

the code i used...

   $("ol#newlist > li").show();


            for (var i = 0; i < $("ol#newlist > li").length; i++)
            {
                if ($("ol#newlist > li:eq(" + i + ") ol > li").length > 1)

                $("ol#newlist > li:eq(" + i + ") ol > li").show();


            }

the sample page here

Now i want all the list in a single variable like i can get the lis in a variable...

      var $li = $("ol#newlist > li");

but the code

      $li.add($("ol#newlist > li:eq(" + i + ") ol > li"));

is not working...

the sample page here

the sample page has been updated... the answer should be....

     var $li = $("ol#newlist > li").add(
                    $('#newlist').children('li').children('ol').filter(function() {
                        return $(this).children().length > 1;
                    }).children()
            );
     $li.show();

or

  var $li = $('#newlist').find('li').filter(function() {
                return ($(this).siblings('li').length );
            });
  $li.show();

as answered by patrik...

Thanks for the help...

Thanks
Pradyut
India

A: 

I used the .each to run the function on each ol
I think this is what you are trying to do
hope this helps!

$("#newlist").show();
    var $li = new Array();
    $("#newlist > li").each(function(index){
        if($("> ol > li", $(this)).length > 1){
            $("> ol", $(this)).show();
            $("> ol > li", $(this)).each(function(){
                $li.push($(this));
            });
        } else {
            $("> ol", $(this)).hide();
        }   
    });

$($li).each(function(){
    alert($(this).text());
});
Kumu
+1  A: 

Sorry if this isn't what you're looking for, but the result you want has the li always showing, with its child ol being hidden,

Test
   1
   2
   3
Another test
Cool Test
   1
   2

But your css hides the li elements for some reason.

#newlist li {
        display:none;
        list-style: none;
    }

If you instead hid the ol elements

#newlist ol {
    display:none;
}

You could simply do a filter, and show them as needed.

    $('#newlist').show().find('ol').filter(function() {
        return $(this).children().length > 1;
    }).show();

Otherwise, if you can't change your CSS, you need to do a little more work making sure everything gets shown properly.

Something like:

$('#newlist').show().children('li').show().children('ol').filter(function() {
        return $(this).children().length > 1;
    }).children().show();

EDIT:

There are probably several ways to add what you want to a collection.

Here's one. It grabs all the li elements, then filters them so only those that have at least one sibling are kept.

The filter is applied to the top-level li elements too, but that's alright since there's more than one of them ( they have siblings ).

var $collection = $('#newlist').find('li').filter(function() {
    return ($(this).siblings('li').length );
});

$collection.show();

Since filter() returns the result of a boolean test, doing

return ($(this).siblings('li').length );

returns true if length is greater than 0.

There are, (I'm sure) plenty of other ways, but this one seems pretty concise. Seems a little better than my original answer too.

patrick dw
The last code works right...thanks...but can i return it in a variable ($li) and then show using $li.show()... thanks...
Pradyut Bhattacharya
thanks for the help... that code was right...
Pradyut Bhattacharya
ok so siblings is a zero based index then...right???
Pradyut Bhattacharya
`siblings()` actually returns a collection of jQuery objects. If you access them by index, then yes, it would be zero based. If you call `.length` against the collection, then you are getting back a quantity. In the bottom example, each `li` element is being test to see how many siblings it has. This line `return $(this).siblings('li').length` will return `true` if there are 1 or more siblings, or will return `false` if there are no siblings. (The number of siblings, of course, does not include the `li` being tested. Only its siblings.)
patrick dw
A: 

I can get the childrenin a variable using the code ...

var $li = $('#newlist').children('li').show().children('ol').filter(function() {
       return $(this).children().length > 1;
}).children();

 $li.show();

But still cannot the whole list(the required output) in a single variable...

if i can get it in a single variable i can use this code...

function animate_li(){
                   $li.filter(':first')
                      .animate({
                         height:  'show',
                         opacity: 'show'
                      }, 100, function(){
                        animate_li();
                      });
                  $li = $li.not(':first');
                }

Please help

thanks

Pradyut

Pradyut Bhattacharya
I added an update at the bottom of my answer that stores the elements in a collection. *Just so you know, it is better to edit your original question if you want to add more info, instead of putting it in an answer.*
patrick dw
thanks for the edit... i could not add so much in the original question.. thats why..
Pradyut Bhattacharya
now here goes my version...
Pradyut Bhattacharya