 <p>The scope of <code>count</code> is just the single child you are processing. Move it to a higher scope:</p> <pre><code>$(xml).find('parent').each(function () { var body = $('body').addClass('nobg'); var count = 1; $(xml).find('child').each(function() { if ($(this).attr('title') &gt; '') { var title = $('&lt;p&gt;&lt;/p&gt;').appendTo(body); headline.attr('id'), (count++)); } }) }) </code></pre> <p>Also, to be on the safe side, I would replace <code>count++</code> with <code>++count</code> as you need it to increment before it is used. This is pure readability, though, in your case.</p>