Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.55 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jQuery.each - Getting li elements inside an ul
  2. <div class="phrase">
  3.     <ul class="items">
  4.         <li class="agap"><ul><li>TEXT1</li></ul></li>
  5.         <li class="agap"><ul>  </ul></li> <!-- empty ul -->
  6.         <li class="aword">TEXT2</li>
  7.         ..
  8.     </ul>
  9. </div>
  10.  
  11. <div class="phrase"> ... </div>
  12.        
  13. var string = "TEXT1 - BLANK - TEXT2";
  14.        
  15. <script>
  16. $(function() {
  17.     $('.phrase .items').each(function(){
  18.         var myText = "";
  19.  
  20.         // At this point I need to loop all li items and get the text inside
  21.         // depending on the class attribute
  22.  
  23.         alert(myText);
  24.  
  25.     });
  26. };
  27. </script>
  28.        
  29. // note this array has outer scope
  30. var phrases = [];
  31.  
  32. $('.phrase').each(function(){
  33.         // this is inner scope, in reference to the .phrase element
  34.         var phrase = '';
  35.         $(this).find('li').each(function(){
  36.             // cache jquery var
  37.             var current = $(this);
  38.             // check if our current li has children (sub elements)
  39.             // if it does, skip it
  40.             // ps, you can work with this by seeing if the first child
  41.             // is a UL with blank inside and odd your custom BLANK text
  42.             if(current.children().size() > 0) {return true;}
  43.             // add current text to our current phrase
  44.             phrase += current.text();
  45.         });
  46.         // now that our current phrase is completely build we add it to our outer array
  47.         phrases.push(phrase);
  48.     });
  49.     // note the comma in the alert shows separate phrases
  50.     alert(phrases);
  51.        
  52. // outer scope
  53. var phrases = [];
  54.  
  55. $('.phrase').each(function(){
  56.     // inner scope
  57.     var phrase = '';
  58.     $(this).find('li').each(function(){
  59.         // cache jquery object
  60.         var current = $(this);
  61.         // check for sub levels
  62.         if(current.children().size() > 0) {
  63.             // check is sublevel is just empty UL
  64.             var emptyULtest = current.children().eq(0);
  65.             if(emptyULtest.is('ul') && $.trim(emptyULtest.text())==""){
  66.                 phrase += ' -BLANK- '; //custom blank text
  67.                 return true;  
  68.             } else {
  69.              // else it is an actual sublevel with li's
  70.              return true;  
  71.             }
  72.         }
  73.         // if it gets to here it is actual li
  74.         phrase += current.text();
  75.     });
  76.     phrases.push(phrase);
  77. });
  78. // note the comma to separate multiple phrases
  79. alert(phrases);
  80.        
  81. $(function() {
  82.     $('.phrase .items').each(function(i, items_list){
  83.         var myText = "";
  84.  
  85.         $(items_list).find('li').each(function(j, li){
  86.             alert(li.text());
  87.         })
  88.  
  89.         alert(myText);
  90.  
  91.     });
  92. };