I have a table and I am adding rows to it dynamically, every one of the rows will have 2 cells, one of them will contain an input field where I plan to use the DatePicker UI to, obviously, pick a date. The issue comes when I add the new row, cells and everything add just fine but the input field somehow does not show the DatePicker when I click on it. Reading around I learned I'm supposed to use "Event Delegation" since the new elements were not loaded from the start. But for the life of me I have not been able to understand how to do it, if someone could point me in the right direction that would be great. Thanks
The code I've got so far:
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery- ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$( ".datepicker" ).datepicker();
$( "#TheTable" ).on("change",".datepicker",function(){
$( this ).datepicker( "option", "dateFormat", "yy-mm-dd" );
});
});
function addRow() {
var table = document.getElementById("TheTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML='Date';
cell2.innerHTML='<input type="text" class="datepicker"/>';
}
</script>
</head>
<body>
<table name="MyTable" id="TheTable">
<tr>
<td>Date</td>
<td><input type="text" class="datepicker" /></td>
</tr>
<tr>
<td>Date</td>
<td><input type="text" class="datepicker" /></td>
</tr>
</table>
<input type="button" onClick="return addRow()" value ="Add Another Date">
</body>
</html>