5

http://jsfiddle.net/YsnhT/2/

Jquery event on is not working after the append. I need the value of textarea after I click save button.

$('.span8')
    .on('click',
            '.btn',
            function() {
        var input = $("#textarea").val();
        alert(input);
    });

$('body').on('click','#createNote',function() {                $('.span8').empty();
                $('.span8').append('<button type="button" class="btn" style="float: right; margin-right: 8px;background-color:#dddddd;font-family:Roboto Slab;color: black" id="save" data-loading-text="Saving...">Save</button><div class="hero-unit"><input type="text" style="width: 100%" id="title" placeholder="Title"/>'+ 
                '<textarea contenteditable="true" id="textarea" class="textarea" placeholder="Enter text ..."style="width: 100%;">dd</textarea></div>');

            });

HTML:

<div class="span8"></div>
shashank
  • 326
  • 4
  • 6
  • 19
  • possible duplicate of [Events triggered by dynamically generated element are not captured by event handler](http://stackoverflow.com/questions/12829963/events-triggered-by-dynamically-generated-element-are-not-captured-by-event-hand) – Felix Kling Mar 02 '14 at 19:17

5 Answers5

17

Since #save is dynamically created, use the following handler:

$(document).on('click', '#save' ,function() {
    //do stuff
});

Updated fiddle: http://jsfiddle.net/tymeJV/YsnhT/3/

You should also make your "click" button handler like this, or else it will not fire on newly created #click buttons.

tymeJV
  • 103,943
  • 14
  • 161
  • 157
4

Simply delegate the event in dynamic format:

$(document).on('click', '#save' ,function() {
    //do stuff
})

And perhaps do the same for:

$(document).on('click', '#click' ,function() {
    //do stuff
})
flavian
  • 28,161
  • 11
  • 65
  • 105
1

save is creating dynamically here.So try to using on

$(document).on('click', '#save' ,function() {
});
PSR
  • 39,804
  • 41
  • 111
  • 151
0

Once you have appended the element you need to rebind the handlers to it or use jQuery's live() function, like so:

$('.span8 .btn').live('click', function() {
    var input = $("#textarea").val();
    alert(input);
});
Maloric
  • 5,525
  • 3
  • 31
  • 46
0

check this...

$('.btn').click(function() {

 var input = $("#textarea").val();
    alert(input);

});

mathew
  • 193
  • 1
  • 5