id for each element showing undefined or 0 not being assigning different id's
<p>I'm trying to assign a id to each link in my list like this,</p>

<pre><code>for (var j = start; j &lt; stop; j++) { 
    link = linkBase + json[j].relatedItemId;

    $('#citations').append('&lt;li&gt;&lt;a href="' + link + '" id="num" + j&gt;' + 
        json[j].title + '&lt;/a&gt;&lt;/li&gt;'); 
        alert($('a').attr('id'));
} 
</code></pre>

<p>it keeps giving me undefined or 0? Should I use $.each outside of the for loop instead?</p>

<p>I was trying to use the for loop for two purposes but maybe that's not such a great idea?</p>

<p><em><strong></em>*EDIT***</strong></p>

<p>If I put my for loop inside of a function like,</p>

<pre><code>// Loop function for each section
var loopSection = function(start, stop) {

// Http setup for all links
var linkBase = "http://www.sciencebase.gov/catalog/item/";

// Link for citation information
var link = "";

for (var j = start; j &lt; stop; j++) { 
    link = linkBase + json[j].relatedItemId;

    var $anchor = $("&lt;a&gt;", {
        href: link, 
        id: "id" + j, 
        text: json[j].title
    })   

    // .parent() will get the &lt;li&gt; that was just created and append to the first citation 
    element                                              
    $anchor.appendTo("&lt;li&gt;").parent().appendTo("#citations");
    }
}
</code></pre>

<p>I'm not able to access the id from outside of the function</p>

<pre><code>$('#citations').on('click', function (e) {
    e.preventDefault();

    var print = ($(this).attr('id'));

    alert(print);
});
</code></pre>
