2

I am trying to find id of ul which doesnot have any li element in code below:

if ( $("#myDiv ul.myClass > li").length < 1 ){
     alert($(this).attr("id"));
    }

I also saw this article but it uses .each() function. I want to achieve this without using any looping. Is this possible?

JSFiddle link

In the JSFiddle above, I am unable to get the ID of the empty UL element. It is alerting that it is undefined.

Could anyone please let me know what I am doing wrong here?

Community
  • 1
  • 1
Mrudang Vora
  • 255
  • 1
  • 5
  • 20

2 Answers2

2

In your posted jsFiddle you have a bad selector to start. You want the .myClass to come before the UL.

If you fix that, then all you have to do is:

alert($("#myDiv > .myClass > ul:empty").prop('id'));

jsFiddle

There are simpler derivations of the selector that would also work:

  • #myDiv .myClass ul:empty - No Immediate inheritence
  • .myClass ul:empty - Any instance of ul:empty that is a descendant of .myClass
  • #myDiv ul:empty - All empty ul's in myClass
Daniel Gimenez
  • 18,530
  • 3
  • 50
  • 70
  • Actually, I wanted to select all the UL elements under element with class = "myClass". Hence, I wrote ul.myClass so that I am able to get all UL elements. Thanks for your response. – Mrudang Vora Mar 03 '14 at 17:56
  • Based on your example `ul.myClass` will always be empty. My selector will select all UL elements that are under an element with `.myClass` that are under `#myDiv`. Of the examples, the second is the closest you've described, just add the `>` in between if you want directly below. – Daniel Gimenez Mar 03 '14 at 18:00
  • The jsFiddle works fine but my application uses jquery 1.4.2 in which I am still unable to find the id. It shows undefined when I used .attr("id"). Could you please help? – Mrudang Vora Mar 04 '14 at 10:49
1

You could check using :empty selector:

 alert($("#myDiv ul:empty").attr("id"));

But if more than one, this will returns only ID of the first matched one.

EDIT: your HTML is not valid in jsFiddle. LI cannot be direct child of DIV.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • Yeah you are correct. I just created sample fiddle without noticing that LI cannot be direct child of DIV. Thanks for your response. – Mrudang Vora Mar 03 '14 at 17:49
  • The jsFiddle works fine but my application uses jquery 1.4.2 in which I am still unable to find the id. It shows undefined when I used .attr("id"). Could you please help? – Mrudang Vora Mar 04 '14 at 10:50
  • even using jq 1.4.2, code works. So your issue is coming from somewhere else. Check your console. But its quite strange you accepted answer using `.prop()`, jq 1.4.2 doesn't support it... – A. Wolff Mar 04 '14 at 10:53
  • The fiddle was working fine. Hence, I accepted the solution. But, I saw that .prop is not supported in jq 1.4.2. Hence, I used your .attr instead of .prop. But not sure, why it is not working in my application – Mrudang Vora Mar 04 '14 at 11:04
  • @MrudangVora so, is it working now? See working demo, using jq 1.4.2: http://jsfiddle.net/QJt6v/4/ – A. Wolff Mar 04 '14 at 11:04
  • No it isn't. I tried to alert length `alert($("#myDiv .MyClass > ul:empty").length);` which is coming 1 in fiddle but in my app it is coming as 0. – Mrudang Vora Mar 04 '14 at 11:17
  • That's means element not available in DOM at that time or selector wrong. Now you have to provide more relevant code in question as HTML markup and maybe online link where your issue can be seen. BTW, is it dynamic elements you are tageting? – A. Wolff Mar 04 '14 at 11:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48953/discussion-between-mrudang-vora-and-a-wolff) – Mrudang Vora Mar 04 '14 at 11:23