views:

525

answers:

2

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

+1  A: 

Why not just use .length instead?

$('#fileTreeInviati .php-file-tree .pft-directory li a.new').length;

Update: If you want to count every li element separately, use this:

$('#fileTreeInviati .php-file-tree .pft-directory li').each(function() {
 alert($('a.new', this).length);
})
Mathias Bynens
Ok sorry, I've got your point..length count all the a.new I need them splitted by ul...every ul must have his counter separately.
Kreker
@Kreker: I updated my post.
Mathias Bynens
same result...always got "1"
Kreker
posted an answer
Kreker
A: 

OK, I figure out how to do the magic:) Here it is:

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

Now all works perfect. If you think this can do in a better way, lemme know. Bye

Kreker