I'm having a heck of a time with this. I'd like to reorganize these <span>
elements within each <td class="ref_type_block">
, sorting by the attribute sort_order
.
<tr>
<td class="time_col">11am</td>
<td class="ref_type_block">
<span class="ref_type_1" sort_order="0">█</span>
<span class="ref_type_2" sort_order="1">█</span>
<span class="ref_type_3" sort_order="3">█</span>
<span class="ref_type_13" sort_order="2">█</span>
</td>
</tr>
<tr>
<td class="time_col">12pm</td>
<td class="ref_type_block">
<span class="ref_type_1" sort_order="0">█</span>
<span class="ref_type_2" sort_order="1">█</span>
<span class="ref_type_3" sort_order="3">█</span>
<span class="ref_type_13" sort_order="2">█</span>
<span class="ref_type_13" sort_order="2">█</span>
<span class="ref_type_13" sort_order="2">█</span>
</td>
</tr>
I have the following javascript function which feels very close, I can log things to console showing me it's selecting the right parts, and .sort()
is firing, but it's not rewriting to the <td>
element.
function sortBlocks() {
var tds = $('.ref_type_block');
for (var i = tds.length - 1; i >= 0; i--) {
td = tds[i]
console.log( $(td).find('span').length );
if ($(td).find('span').length > 0) {
console.log("firing on:",td);
$(td).find('span').sort(function (a, b) {
return +a.dataset.sort_order - +b.dataset.sort_order;
}).appendTo( $(td) );
}
};
}
I've been away from javascript / jquery for a bit, afraid it's something terribly simple, particularly around the .appendTo()
method. Any help would be appreciated.