I am trying to sort html elements with jQuery based off the order of items in a data attribute on an input. "data-order"
See the code setup here, but I am stuck.
http://jsbin.com/cixolonoqe/1/edit?html,js,output
HTML:
<div id="control">
<input type="hidden" data-order="title,text,date">
</div>
<div id="parts">
<p class="date part">20 January 2014</p>
<h5 class="title part">Some Title</h5>
<p class="text part">A line of Text</p>
</div>
jQuery:
var input = $('#control input');
var partsOrder = input.attr("data-order");
var parts = $('#parts');
parts.find('.part').each(function(i) {
$(this).attr('data-index', 'index' + i);
});
//Make array from data-order attribute
var partsArray = partsOrder.split(',');
$.partsArray(json, function(i, val) {
//modify html order
});
If data-order string changes, I want the html to re order accordingly.
title,text,date vs date,text,title
So far I have added a new data attribute with the original index onto the parts. And created an array from the data-order string so it can be looped through and modify the html.
But I am unable to get my head around it.