html - jQuery ignores last element in returned set -
i'm working on internal plugin jquery datatables, , i'm experiencing weird behavior.
take @ following fiddle: http://jsfiddle.net/bjllz/
the code below looking every 'tr' first 'td' element, , adds 'td' checkbox input before every match finds. (notice last row in fiddle above not have checkbox)
$(function () { var checkbox = $('<input>', { "type": "checkbox" }) var td = $('<td/>', { 'class': "table-checkbox" }).insertbefore("tbody > tr > td:first-child") var checkboxes = $(checkbox).appendto(td) var th = $('<th/>', { 'class': 'text-center' }).insertbefore("thead > tr:nth(0) > th:nth(0)") $(checkbox).appendto(th) .change(function () { $(this).is(":checked") ? checkboxes.prop('checked', true) : checkboxes.prop('checked', false); }) })
although jquery adding relevant 'td' element, last 'td' in table added, without checkbox input..
this unexpected.. 1 have idea why ?
this line it:
$(checkbox).appendto(th)
unlike .insertbefore()
, .appendto()
moves element new position. after other operations before line checkbox
refers checkbox on last row of table.
you need make copy of checkbox before appending it:
$(checkbox).clone().appendto(th)
Comments
Post a Comment