jQuery function looping or not ending
<p>My basic function here brings up a popup when you click on an image, and allows you to browse through a list of friends and swap the current picture on the page with one from the popup. The swap works fine, but my function seems to fire multiple times - I know this, because the d variable shows up in the console multiple times. On the first use, it shows up once. On the second, it shows up twice, etc.</p>

<p>I imagine it has something to do with the $('#friendlist li').click function inside of the main loop, and that it is either not "shutting down" once the picture gets replaced, or something to do with variable scope.</p>

<pre><code>$('img[name=pop]').click(function(e) {
        //pop up
    var p = $(this);
    var position = p.position();
    var offset = p.offset();
    $('#friendlist').css("left", position.left + $(this).width() );
    $('#friendlist').css("top", 0);
    $('#friendlist').toggle();
    //console.log( this );
    //console.log( position.top + " " + offset.top + " " + e.pageY );       

        //listener for the friendslist, retrieves the uid of the friend chosen
    var pointer = $(this).attr("id");
    var c = 0;
    console.log( "before c: " + c );
    $('#friendlist li').click(function( ){
        var d = 0;
        console.log( "inner d: " + d );
        if( d &lt; 1){
            pictureReplace( pointer , $(this).attr("id") ); //function that swaps the img
            $('#friendlist').hide();
            d++;
        }
    });
});
</code></pre>

<p>Here is the basic HTML for the page:</p>

<pre><code>&lt;div id="main_page"&gt;

&lt;div&gt;
    &lt;img src="https://graph.facebook.com/&lt;?php echo $friendList[0]['id']; ?&gt;/picture" name="pop" id="friend1" /&gt;
    &lt;?php echo $friendList[0]['name']; ?&gt;
&lt;/div&gt;

&lt;div&gt;
    &lt;img src="img/&lt;?php echo $friendList[1]['id']; ?&gt;" name="pop"  id="friend2" /&gt;
    &lt;?php echo $friendList[1]['name']; ?&gt;
&lt;/div&gt;

&lt;div&gt;
    &lt;img src="img/&lt;?php echo $fb_me['id']; ?&gt;" /&gt;
    &lt;?php echo $me['name']; ?&gt;
&lt;/div&gt;

&lt;div&gt;
        &lt;img src="img/&lt;?php echo $friendList[2]['id']; ?&gt;" name="pop" id="friend3" /&gt;
        &lt;?php echo $friendList[2]['name']; ?&gt;
&lt;/div&gt;

&lt;?php //create the friends list for the search ?&gt;
&lt;div id="friendlist"&gt;
    &lt;div&gt;People&lt;/div&gt;
    &lt;input type="text" id="fsearch" name="fsearch" class="" /&gt;
    &lt;ul id="friends_ul"&gt;
    &lt;?php
        $li_num = 1;
        foreach($friendList as $key =&gt; $val){
    ?&gt;
        &lt;li id="li_id_&lt;?php echo $li_num; ?&gt;"&gt;
            &lt;img src="img/&lt;?php echo $val['id']; ?&gt;" /&gt;       
            &lt;label&gt;&lt;?php echo $val['name']; ?&gt;&lt;/label&gt;
            &lt;input type="hidden" name="hiddenid" value="&lt;?php echo $val['id']; ?&gt;"&gt;
        &lt;/li&gt;       
    &lt;?php
        $li_num++;
    }
    ?&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</code></pre>
