I'm writing a small userscript to include a link next to user's profile image for a phpBB forum that I frequent. On clicking the link, I'm getting an error like below:

```
Uncaught ReferenceError: doit is not defined
(anonymous function)            viewtopic.php:542
onclick                         viewtopic.php:543

```

Portion of the userscript:

```
(function(){

    var script = document.createElement('script');
    script.textContent = '(' + twk.toString() + ')();';
    document.body.appendChild(script);

    function twk() {

        pd = document.getElementsByClassName('postdetails');

        for (i=0 ; i<(pd.length); i++) {
            ele = document.createElement("a");
            ele.innerHTML ='<a href=\'#\' onclick=\'doit();\'>Quick reply</a>';

            pd[i].appendChild(ele);

        }

   function  doit() {
        selec = document.getSelection().anchorNode.textContent;
        document.getElementsByClassName("row2").item('message').innerHTML = selec;

    }

}

})();

```

Can anyone please point out where/what am I doing wrong?