Question: I am trying to add an element to the DOM, and then working with this element, once clicked. However, I don't think the function below registers because this is a self invoking function (located at the end of the HTML), and is run only at the beginning.

```
 (function (){
    $('body').append('<span class="test">test</span>');
})

 $('.test').click(function(){
     console.log(this);
 });

})();

```

the click event handler is not working.

Answer: 
or you can do this,

```
$(function() {
    $('body').append('<span class="test">test</span>');
    $('.test').click(function() {
        console.log(this);
    });
}); 

```

though `live` or `on` would be the better alternative if you don't know in advance when that element is going to get added to the DOM.
You were closing the function just after the append part thats why click handler wasn't working

```
$(function() {
    $('body').append('<span class="test">test</span>');
    $('.test').click(function() {
        console.log(this);
    });
})(); 

```