Making span clickable after prepend in jQuery

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-05-26T10:53:01Z Indexed on 2010/05/26 11:01 UTC
Read the original article Hit count: 257

Filed under:

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.

© Stack Overflow or respective owner

Related posts about jQuery