Adding a click() to a dynamically generated link : <p>I am trying to call a function from a dynamically generated button. However the click() function is not working when I click the link. Can't figure out why. </p>

<pre><code>if ( hasCap === 'image' ) {
  caption = $(this).data('caption');
  link    = '&lt;a href="#" style="float: right; margin-top: -10px;" class="hidecaption"&gt;Hide Caption&lt;/a&gt;';

  return link + (caption ? caption + '&lt;br /&gt;' : '') ;
}

  $( ".hidecaption" ).click(function() {
      alert( "target called" );
    });
</code></pre>
