I have dynamic fields which can be added by users. The fields contain of a selection field and input field. I want to put on change event on the selection field but failed. I tried using class name but nothing happen.
This is the dynamic added field code:
<div class="form-group">
<div class="row">
<input type="button" class="add-row" value="Add Row">
<button type="button" class="delete-row">Delete Row</button>
</div>
</div>
<script>
count=1;
$(document).ready(function(){
$(".add-row").click(function(){
var markup = '<tr><td><input type="checkbox" name="rekod" style="width:20px"></td>';
markup += '<td><select class="nama" name="nama[]" style="width:320px"><option value="">Pilih</option>
<?php foreach($nama as $key => $value):echo '<option value="'.$key.'">'.addslashes($value).'</option>'; endforeach; ?></select></td>';
markup += '<td><input type="text" class="no" name="no[]" style="width:150px"></td>';
markup += '<td class="col-lg-1"><input type="text" class="form-control bahagian" name="bahagian[]" style="width:100px"></td>';
markup += '<td class="col-lg-1"><input type="text" name="telefon[]" style="width:100px"></td>';
markup += '<td class="col-lg-1"><input type="text" name="ext[]" style="width:100px"></td></tr>';
$("table tbody").append(markup);
count++;
});
$("select.nama").change(function() {
alert('done it');
});
// Find and remove selected table rows
$(".delete-row").click(function(){
$("table tbody").find('input[name="rekod"]').each(function(){
if($(this).is(":checked")){
$(this).parents("tr").remove();
}
});
});
});
</script>
I am using jquery 1.12.4. Can anyone help me to trigger the on change event? Without dynamic field, I am able to trigger the event.