jquery - Add a method to an object after .append() -
i'm creating table lines can added/deleted.
the js part looks this:
$('#absence-table').on('click', '#delete-line',function() { $(this).closest('tr').remove(); }); $('#add-line').click(function(e) { e.preventdefault(); $('#absence-table').append([ '<tr>', '<td>', '<select name="absence-type" class="input-small">', '<option value="t1">type1</option>', '<option value="t2">type2</option>', '</select>', '</td>', '<td><input type="text" id="from" class="input-small" /></td>', '<td><input type="text" id="to" class="input-small" /></td>', '<td><div class="text-center"><input type="text" id="result" class="input-xsmall" /></div></td>', '<td><div class="text-center"><input type="checkbox" id="morning"></div></td>', '<td><div class="text-center"><input type="checkbox" id="afternoon"></div></td>', '<td colspan="2"><input type="text" id="comment" /></td>', '<td><a href="#" id="delete-line"><i class="icon-remove"></i></a></td>', '</tr>' ].join('')) }); $("#from, #to").datepicker();
my first problem make works "click" event on line created .append() function. after search, found need use .on() , it's work.
but datepicker on new table line created doesn't work. how can do?
$("#from, #to").datepicker(); // seems not working after .append() on new line
you need attach datepicker
plugin newly added html. attached elements available on load.
$('#add-line').click(function(e) { e.preventdefault(); $('#absence-table').append(/* super long string */) $("#from, #to").datepicker(); // attach new elements }); $("#from, #to").datepicker(); // attach on load
you dry bit putting datepicker
instantiation in function.
Comments
Post a Comment