Skip to content Skip to sidebar Skip to footer

Where Is The Error In Sorting Code

Hey everybody please help I can't find the error when I press sort...it sort again and again forever html:

Solution 1:

That's because you are sorting one element by selecting the first element $('div.labelimagediv')[0];, try this:

$("button.btnSort").click(function () {
    $('div.labelimagediv').sort(function(a, b) {
       return a.getAttribute('rel') > b.getAttribute('rel');
    }).appendTo(".unsorted");
});

JsFiddle fork here

Solution 2:

var item1 = $('div.labelimagediv')[0];

^^ that's just one single element ?

It should be :

$("button.btnSort").click(function () {
    var sortedDivs = $('.labelimagediv', '.unsorted').get();
    sortedDivs.sort(function(a,b) {
        return a.getAttribute('rel') - b.getAttribute('rel');
    });
    $.each(sortedDivs, function (index, value) {
        $(".unsorted").append(value);
    });
});

FIDDLE

Post a Comment for "Where Is The Error In Sorting Code"