I have a strange problem with Date Picker. He only works on the first row on a dynamic table.
$(document).ready(function () {
$(".Data1").datepicker({
showOn: "button",
buttonImage: "/calendar.png",
buttonImageOnly: true,
changeMonth: true,
changeYear: true
});
});
Here its my Javascript for creating a new row:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
newcell.className = "clastest";
if (i == 3) {
newcell.getElementsByTagName("input")[0].id="StartD"+rowCount;
}
if (i == 4) {
newcell.getElementsByTagName("input")[0].id = "EndD" + rowCount;
}
if (i == 5)
{
newcell.getElementsByTagName("input")[0].id =rowCount;
}
}
}
And here is the HTML
<td>
@Html.TextBox("Pozition", poz, new { @style = "width:50px;", @id = "Pozition" })
</td>
<td>
@Html.TextArea("Description", des, new { @style = "width:250px; min-height:50px;", @id = "Description" })
</td>
<td>
@Html.TextBox("StartDate", di, new { @class = "Data1", @id = "StartD" })
</td>
<td>
@Html.TextBox("EndDate", de, new { @class = "Data1", @id = "EndD" })
</td>
Any ideas why it works only on the first row and not on all?
The problems is that you’re are not adding the event to the newly created elements, when you add the datepicker function only works for the first row because it’s the only created when the dom is ready, you’ll need to add the event again each time you add a new row.
You have 2 options:
Event binding on dynamically created elements?
Then add
addDatePicker()at the end ofaddRow()function