tags:

views:

1277

answers:

6

I have code like above

     ` <ul id="jsddm">

                            <li><a href="Default.aspx">
                                Menu</a>
                                <ul style="visibility: hidden;">


                                            <li><a href="Content.aspx?ID=153">
                                                SubMenu</a></li>


                                            <li><a href="Content.aspx?ID=152">
                                                SubMenu</a></li>

                                </ul>
                            </li>

                            <li><a href="#">
                                Menu</a>
                                <ul style="visibility: hidden;">

                                </ul>
                            </li>

                </ul>
            </div>`

I want to romove empty sub "ul" tags which is has got not "li" tag(s) with jquery when my page onload. How can I do? Thanks for help.

A: 

I think:

$('li ul:empty').remove();

will do what you want.

Mark B
Thank you, but it did not work.
Kerberos
This would work if it weren't for the fact you have a lot of whitespace in your UL element. This makes the `:empty` selector return false.
Manticore
I could not understand what it did not work. if you want i can send all html output codes.
Kerberos
i think empty means "if tag has got not attribute" does is? if which i said, fact question may be like this; i want remove ul tag which it has'nt got <li> tag(s)!
Kerberos
A: 

Try this:

$(document).ready(function () {
    $("#jsddm ul").empty();
});
pj.az109
Thank you too, This code removed all ul tags under jsddm's. But i want to remove only empty ul tags.
Kerberos
+3  A: 

The jQuery :empty selector would work if your UL elements really were empty, and didn't contain any white space.

To check for both empty and elements with white space you could do something like this:

$("#jsddm ul").each(function(i, el) {
    // Remove white space
    var html = $(el).html().replace(/\s+/, "");

    // Check if element is :empty or length of html is zero
    if ($(el).is(":empty") || html.length == 0)
        $(el).remove();
});
Manticore
Thank you, it works perfect.
Kerberos
Just check the child count like Zed and Aistina did... that's just overly complicated
maisteri
Yes, I used Zed's proposal for my issue as you said. Thank you.
Kerberos
+3  A: 

I think this is what you're looking for:

$('#jsddm ul').each(function() {
  if ($(this).children().length == 0) {
    $(this).remove();
  }
});

It removes all the <ul> elements under the element with id jsddm which do not contain child nodes.

Aistina
Great idea to check the children count!
Manticore
+2  A: 
$("ul").each(
  function() {
    var elem = $(this);
    if (elem.children().length == 0) {
      elem.remove();
    }
  }
);
Zed
Yes, this method works perfect too as Manticore's proposal. Thank you very much for your help too.
Kerberos
+5  A: 
$('ul').not(':has(li)').remove();
Alex Barrett
my fav, the smallest!
SeanDowney