With my very limited knowledge of both jQuery and JS i made a small script (with the help of Nicola Peluchetti) that duplicates a tablerow when a button is clicked. This works like a charm, however, i would like to add a datepicker to it. This also works well, but just once and not on the copied fields or visa versa. It’s probably because the datepicker “thinks” there is just one field.
I found several sources to solve the problem, but again, with my newbie knowledge, it’s just to hard. I am playing with this for almost two days and can’t get it solved.
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("input[type='button'].AddRow").click(
function() {
var index = $(this).closest('tr').index();
if (index > 0) {
$(this).closest('tr').remove();
} else {
$(this).closest('tr').clone(true).prependTo($(this).closest('table'));
$(this).val(i++);
/* $('.startdatum').removeClass('hasDatepicker').datepicker({dateFormat: 'mm-dd-yy'}); */
}
});
});//]]>
</script>
HTML
<table width="960" border="0" align="center" cellpadding="10" cellspacing="0" class="contentblock">
<tr>
<td width="140"><strong>Startdatum</strong></td>
<td><input name="startdatum[]" type="text" class="startdatum" value="dd/mm/jjjj" />
<script>
$(function() {
$('.startdatum').removeClass('hasDatepicker').datepicker({
dateFormat: 'mm-dd-yy'
});
});
</script>
<select name="locatie[]" id="locatie[]">
<option value="" selected></option>
</select></td>
<td width="143"><strong>Dekking</strong></td>
<td width="133"><select name="dekking[]" id="dekking[]">
<option value="" selected></option>
</select></td>
<td width="145"><input type="Button" value="Add Row" class="AddRow"></td>
</tr>
</table>
Potential solutions that i don’t get:
- http://www.stemkoski.com/problem-with-jquery-ui-datepicker-dynamic-dom/
- duplicating jQuery datepicker
- Dynamic JQuery date picker code
Datepickerplugin:
Could anyone please help me out?
one solution is to destroy the datepickers and rebuild them after adding different ids to them. I changed your code a little:
fiddle here http://jsfiddle.net/nicolapeluchetti/87QCx/1/
Edit – if you need to attach the row after all the others:you must change
in