I have a table that I've created dynamically and in each row there is a button that will clear that row. The button works fine in the first row, but doesn't do anything in the following rows. To troubleshoot this, I set the button to give me an alert on click, but only the first button is registering.
I've changed the button to be an ID and a Class, but that doesn't seem to do anything either.
Here is the code:
//dynamically create 4 rows
$(document).ready(function(){
var rowsAdd = $('#addTable tbody >tr');
var rA = rowsAdd.length;
while (rA<5) {
$('#addTable tbody>tr:last').clone().insertAfter('#addTable tbody>tr:last');
rA++;
}
})
//alert on button click
$('.clear').each(function(index){
$(this).on("click",function(){
alert("Button " +index+ " is clicked");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="addTable">
<tbody>
<tr>
<th style="width: 10%">Class #</th>
<th style="width: 30%;">Course Title</th>
<th style="width: 20%;">Catalog Number</th>
<th></th>
</tr>
<div class="addTable">
<tr>
<td class="classNumID"></td>
<td>
<input list="courses" name="courseInput" placeholder="Course" class="courseClass" oninput="UpdateCatNumbers()">
<datalist id="courses" name="courseDatalist">
<!--Filled in script-->
</datalist>
</td>
<td>
<input list="catalogs" name="catalogInput" placeholder="Catalog Number" id="catalogID" class="catClass" oninput="UpdateCourseNames()">
<datalist id="catalogs" name="catalogDatalist">
<!--Filled in script-->
</datalist>
</td>
<td><button class="clear">−</button></td>
</tr>
</div>