0

my have some trouble to call a JavaScript/jQuery 1.9.1 method on dynamic added html-elements.

Here is my JS-code:

$(".download").live("click", function() {
    var buttonId = $(this).attr("id");
    alert(buttonId);
});

My dynamic HTML code:

  var html = "";
    for(var i = 0; i <daten.length; i++) {
       html += "<input id='" + daten[i].pfad + "' class='download' type='image' width='25px' height='25px' src='/download.png'/>";
    }
  $("#table").append(html);

I get no error message, but the method is besides not calling, what is here not correct?

Higune
  • 643
  • 3
  • 13
  • 31

3 Answers3

1

Try using on(), as live() was deprecated and removed in newer versions of jQuery:

$('#table').on('click', '.download', function() {
    alert(this.id);
});

FIDDLE

works for me ?

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

Use .on instead

$(document).on("click",".download",function() {
    var buttonId = $(this).attr("id");
    alert(buttonId);
});
bugwheels94
  • 30,681
  • 3
  • 39
  • 60
0

use like this:

  $(document).on("click",".download",function() {

   //put your code here

  });
Marsh
  • 173
  • 1
  • 14