jquery - Click event is not working for button -
i having problems button btndel
not working when click.
here's script:
$('#btnadd').bind('click', function() { var scount = $("#support-list").find('li').length; var tmpstr = $('#txtaddsupportname').val(); var str = “<li>"; var str += “<img src='../wp-content/plugins/hamajob/admin/assets/image/arrow.png' width='16' height='16' class='handle'/>"; str += "<strong alt="+ scount +">" + tmpstr + "</strong>"; str += "<input type='button' id='btndel'/>"; str += "</li>"; $('#support-list').append(str); }); $('#btndel').bind('click', function(){ alert(this); });
however, script work:
str += "<input type='button' id='btndel' onclick='btndel(this)'/>"; btndel = function(e){ alert(this); }
could explain difference?
the newly added delete button not in dom, thats reason why first example not working. use .delegate()
check updated code jsfiddle
$('#btnadd').bind('click', function() { var scount = $("#support-list").find('li').length; var tmpstr = $('#txtaddsupportname').val(); var str = "<li>"; str += "<img src='../wp-content/plugins/hamajob/admin/assets/image/arrow.png' width='16' height='16' class='handle'/>"; str += "<strong alt="+ scount +">" + tmpstr + "</strong>"; str += "<input type='button' id='btndel'/>"; str += "</li>"; $('#support-list').append(str); }); $( "#support-list" ).delegate( "#btndel", "click", function(){ alert(this); });
Comments
Post a Comment