Tommy
Tommy

Reputation: 697

jQuery wont work when i use append

Im trying to get the glyphicon-cog button clickable

 message = new array();
jQuery.fn.update_textarea = function(test) { 
        /* Reset Areas */ 
        $("#articles_textarea").html('');
        $(".test").html(''); 

        for (i=1;i<=test;++i) {       
            if (message[i]) { 
                $("#articles_textarea").append('<h2 class="SettingsHead" data-id="' + i + '"><input type="text" name="sida[]" value="Sida ' + i + '"><div class="Break"></div></h2><textarea class="ckeditor" id="editor' + i + '"></textarea>'); 
            } else {
                message[i] = '';
                $(".test").append('<tr><td width="150"><a href="#ShowPage' + i + '" data-toggle="tab" class="ShowPage' + i + '">Sida ' + i + '</a></td><td align="right"><span class="glyphicon glyphicon-cog" data-toggle="tooltip" data-placement="left" title="Redigera Sidnamnet"></span></tr>');
                if (i == 1) {
                    $("#articles_textarea").append('<div class="tab-pane active" id="ShowPage' + i + '"><h2 class="SettingsHead" data-id="' + i + '"><input type="text" name="sida[]" class="testing" value="Sida ' + i + '"><div class="Break"></div></h2><textarea class="ckeditor" id="editor' + i + '"></textarea></div>'); 
                } else { 
                    $("#articles_textarea").append('<div class="tab-pane" id="ShowPage' + i + '"><h2 class="SettingsHead" data-id="' + i + '"><input type="text" name="sida[]" value="Sida ' + i + '"><div class="Break"></div></h2><textarea class="ckeditor" id="editor' + i + '"></textarea></div>'); 
                }
            }
            CKEDITOR.replace( 'editor' + i );
        }
    }

But when i use this code

$(".glyphicon-cog").click(function() { alert('y'); });

Nothing happends, it's like im not clicking the button.. Is this because of the append or is it because of something else?

Anyways hope any of you people out there can find the problem and help me find a solution

Upvotes: 0

Views: 66

Answers (1)

Kyle Needham
Kyle Needham

Reputation: 3389

You will need to use .on if you are appending the selector.

$(document).on('click', '.glyphicon-cog', function() { alert('y'); });

Upvotes: 2

Related Questions