I want to add a new row to a table using append by clicking on a image. I use ajax and php to retrieve the new row. After the row is appended the selector onchange does not trigger. Only for the first row.
I checked on Firebug and there is no event on the <select>
How can I make the second row to be triggered on the on change event ?
Code HTML:
<table id="tablines">
<tr>
<td>
<select class="prod" data-id="1" name="product">
<option value="a">A</option>
<option value="b">B</option>
</select>
</td>
</tr>
</table>
<img id="add" src="img/add1.png" style="cursor:pointer">
Code JQuery:
$('.prod').on( "change", function(){
var dataid = $(this).data('id');
alert (dataid);
});
$("img").click(function() {
$.ajax ({
type: "GET",
url: "getrow.php",
success: function(data) {
$('#tablines tbody').append(data);
},
});
});
The PHP return code is:
<tr>
<td>
<select class="prod" data-id="2" name="product">
<option value="a">A</option>
<option value="b">B</option>
</select>
</td>
</tr>
Please try this
Since you are adding the element dynamically you should use delegate(). It attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements