Friday, 15 January 2010

jquery - If ul has has no children, how can I hide or addClass to a span above it? -


So I have jquery style accordion, with jquery UI referenced on page. I am pulling navigation information from a database, and the html structure looks like this:

  & lt; Div id = "accordion" class = "ui-accordion ui-widget ui-helper-reset" role = "tablist" & gt; & Lt; Li class = "sub-item" & gt; & Lt; Div class = "ui-accordion-header ui-helper-reset ui-state-default U-corner-all ui-accordion-icons" role = "tab" id = "ui-accordion-accordion-header-0" area- Control = "lvl2" area-selected = "false" tabindex = "0" & ​​gt; & Lt; Span class = "ui-accordion-header-icon ui-icon ui-icon-triangle-1-e" & gt; & Lt; / Span & gt; & Lt; A href = "/ business / events / upcoming-events" & gt; Upcoming events & lt; / A & gt; & Lt; Span id = "arrow" & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; Ul id = "lvl2" class = "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style = "display: none;" Area-labeling = "ui-accordion-accordion-header-0" role = "tabanel" aria-expanded = "false" aria-hidden = "true" & gt; & Lt; Li class = "lvl2" & gt; & Lt; A href = "/ upcoming-events / meeting-center" & gt; Meeting center & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; Li class = "sub-item" & gt; & Lt; Div class = "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons" role = "tab" id = "ui-accordion-accordion-header-1" Aria- Controls = "lvl2" area-selected = "false" tabindex = "- 1" & gt; & Lt; Span class = "ui-accordion-header-icon ui-icon ui-icon-triangle-1-e" & gt; & Lt; / Span & gt; & Lt; An href = "/ Ebusiness / Meetings / MeetingsCalendar" & gt; Calendar of Events & lt; / A & gt; & Lt; Span id = "arrow" & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; Ul id = "lvl2" class = "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style = "display: none;" Area-labeling = "ui-accordion-accordion-header-1" role = "tabpanel" aria-expanded = "false" aria-hidden = "true" & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; / Div & gt;   

My goal here is to remove the tag, or to add a class that I can again profane: none, if there is no child in the UL # lvl2 list.

This is the jquery function I'm trying to do, but does not seem to execute

  if ($ ('# lvl2: Childland')) {$ ("# Arrow"). AddClass ("blank"); }   

I'm not throwing an error.

Thanks

  if ($ ('# lvl2'). Children () Length) $ ("#Air") AddClass ("blank");   

or with brunch:

  if ($ ('# lvl2'). Children (). Length) {$ ("# arrow") . AddClass ("blank"); }   

The reason for this is that if the length is 1.Infinity , then it is actually valid:

  if (1) {warning ("literally"); }   

but validates 0 :

  if (0) {alert ("farsi"); } And {warnings ("literally"); }   

You ! .

 ! 0 === Using the truth, you can validate the opposite; ! 1 === Wrong; ! False === true; ! True === wrong;   

And then:

  !! 0 === Wrong; !! 1 === true; wrong! === Wrong; !! True === true;    

No comments:

Post a Comment