Making span clickable after prepend in jQuery
- by Saif Bechan
I have a DIV where some code is presented. When I hover on this DIV I want to present a button that toggles the comments inside the code block. So far I have this:
$('.code-block').hover(
function(){
$(this).prepend('<span class="code-block-control">toggle comments</span>');
},
function(){
$('.code-block-control',this).remove();
}
);
$('.code-block-control').click( function(){ $('.comment').toggle(); } );
The span is presented when I hover over the code block. But when I click the span that is created nothing happens. Even if I change the click function to a simple alert nothing happens.
Anyone have any idea on how to fix this.