Skip to content

Instantly share code, notes, and snippets.

@starsinmypockets
Created January 24, 2013 02:31
Show Gist options
  • Save starsinmypockets/4617190 to your computer and use it in GitHub Desktop.
Save starsinmypockets/4617190 to your computer and use it in GitHub Desktop.
Get data attribute from radio input and sort containing div.
/**
* Sort by data-position field
*/
$('.sort-inputs > .form-item').each(function () {
$(this).find('.radio-item').each(function () {
$(this).attr('data-position', function () {
return $(this).find('input[type="radio"]').attr('data-position');
});
});
sortedRadioItems = $(this).find('.radio-item').sort(function (a,b) {
var varA = parseInt($(a).attr('data-position'))
var varB = parseInt($(b).attr('data-position'))
return (varA < varB) ? -1 : (varA > varB) ? 1 : 0;
}).clone();
$(this).find('.radio-item').replaceWith(sortedRadioItems);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment