1

I have to remove a div with an empty ul tag in it.

EDIT: The ul tag is not empty, it has a bunch of whitespace in it.

I want to do this with jQuery

This is what I have so far:

$('trim(#menu):empty').parent().remove();

Where the id of my div is 'menu'.

Example:

before:

<div id="container>
    <div id="menu">
      <ul>          


      </ul>
    </div>
</div>

after:

<div id="container>
</div>

Thanks in advance!

Stijn

2
  • Since when has "trim()" been a selector? Commented Jun 19, 2013 at 20:27
  • :empty won't select a node that contains whitespace, because that's considered a text node. Commented Jun 19, 2013 at 20:30

2 Answers 2

7

Try this:

if($("#menu ul:not:has(li)")) {
    $("#menu").parent().remove();
}
6
  • This doesn't work. Although it seems right... Do I have to add document.onload or something? I'm really a beginner when it comes to JavaScript. Thanks Commented Jun 19, 2013 at 20:45
  • Yes, you need: $(document).ready(function() { //code here }); Commented Jun 19, 2013 at 20:45
  • check my edited post.. That's the BEST way but if it doesn't work try out my first revision. Commented Jun 19, 2013 at 20:48
  • Very nice. It seems so logic. But I can't get it to work :/. jsfiddle.net/yxkHM Commented Jun 19, 2013 at 20:52
  • @Mohammed Areeb Siddiqui: Thanks for your effort, but Claudio Redi fixed it another way around. Commented Jun 19, 2013 at 20:55
2

I'm not sure if I get the idea but this could do the trick

if ($('#menu li').length == 0) {
   $('#menu').remove();
}
1
  • I've edited my question. The ul is not empty, it contains whitspace. Commented Jun 19, 2013 at 20:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.