Strange selector behaviour

Strange selector behaviour

Have a html table with the following structure:

<tr id="SIEcat101"><td colspan="2">Text</td><td id="ID1">0</td><td id="ID12">0</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr id="SIEcat102"><td colspan="2">Text</td><td id="ID2">0</td><td id="ID22">0</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>

<tr class="SIErow"><td>1234</td><td>Text</td><td>2345</td><td>3456</td></tr>


etc etc

For each row with class SIErow I want to create a jsonencoded array with the value of the rows first column (eg 1234) and the id of the first previous row with id beginning with "SIEcat". Here is my code:

  1. var json = {}; 
  2. $(".SIErow").each(function() {
  3. json[$(this).find("td").eq(0).html()] = $(this).prev("tr[id^='SIEcat']").attr("id");
  4.    });
For some reason an array is created with the first "sub-rows" (class = SIErow) after the main rows (ID begins with SIEcat) is created...driving me nuts. I can see that each SIErow is traversed and if I change the code above to:

  1. var json = {}; 
  2. $(".SIErow").each(function() {
  3. json[$(this).find("td").eq(0).html()] = 1;
  4.    });
It creates a complete array...

Any help appreciated.