jquery parent/children selector for counting <li>

Posted by Kreker on Stack Overflow See other posts from Stack Overflow or by Kreker
Published on 2010-05-04T12:41:57Z Indexed on 2010/05/04 12:48 UTC
Read the original article Hit count: 224

Filed under:

Hi. I have this piece of HTML

<div id="fileTreeInviati">
<ul class="php-file-tree">
    <li class="pft-directory">
        <a href="#" class="" name="101">A006 - SOMETEXT (<span name="contaNew"></span>)</a>
        <img src="./moduli/home/images/info.png" title="Informazioni Azienda" class="imgInfo"/>
            <ul style="display: none;">
               <li class="pft-file ext-png">
                    <a href="javascript:getInfoFile('4');" class="" id="4">cut.png</a>
               </li>
               <li class="pft-file ext-dll">
                    <a href="javascript:getInfoFile('27');" class="new" id="27">Safari.dll</a>      
               </li>
            </ul>
    </li>
    <li class="pft-directory">
        <a href="#" class="" name="102">A012 - SOMETEXT (<span name="contaNew"></span>)</a>
        <img src="./moduli/home/images/info.png" title="Informazioni Azienda" class="imgInfo"/>    
        <ul style="display: none;">
        <li class="pft-file ext-jpg">
            <a href="javascript:getInfoFile('19');" class="new" id="19">04.jpg</a>   
        </li>   
        <li class="pft-file ext-dll">
            <a href="javascript:getInfoFile('24');" class="new" id="24">Safari.dll</a>   
        </li>
      </ul>
    </li>
    <li class="pft-directory">
       <a href="#" class="" name="103">A014 - SOMETEXT (<span name="contaNew"></span>)</a>
         <img src="./moduli/home/images/info.png" title="Informazioni Azienda" class="imgInfo"/>    
       <ul style="display: none;">   
         <li class="pft-file ext-txt">
            <a href="javascript:getInfoFile('17');" class="new" id="17">acu.txt</a>   
             </li>   
         <li class="pft-file ext-dll">
              <a href="javascript:getInfoFile('22');" class="new" id="22">Safari.dll</a>  
         </li>
       </ul>
    </li>
</ul>

I'm working on a js snippet that cycle through all "a" of the "li" and checks if it has the class "new" if yes increment a counter by one. This counter now has to be printed on the relative "li" "span" 3 level before. So I have the number of the element with the "new" class. The js snippet is this

$("#fileTreeInviati .php-file-tree .pft-directory li").each(function(){
$(this).children("a").each(function(i,e){
    if ($(e).hasClass("new")){
        cont++;
        console.log($(e).text());
        $(this).parent().parent().parent().children("a").children("span").text(cont);    
    }
})
cont = 0;

});

I think I'm almost there but the counter is always 1. I think there is something mess with .children, maybe it can handle only the first occurrence? Thanks for help

© Stack Overflow or respective owner

Related posts about jquery-selectors