Answer the question
In order to leave comments, you need to log in
What parameters should be added to the button so that the output is from the smallest to the largest element?
function sortable(cls, attr) {
var elements = document.querySelectorAll(cls),
len = elements.length,
parent = document.querySelector('.posts-rub'),
arrElements = [];
for(var i = 0; i < len; i++) {
arrElements.push({
dataAttr : elements[i].getAttribute(attr),
el: elements[i]
});
}
arrElements.sort(function(a, b){
return parseInt(a.dataAttr) > parseInt(b.dataAttr) ? 1 : -1;
});
for(var j = 0; j < len; j++) {
parent.appendChild(arrElements[j].el);
}
}
sortable('.cards', 'data-views');
}
window.onliad = function() {
document.getElementById("lol").onclick = sortable (<b>/* Здесь параметры*/</b>)
}
<div class="cards" data-views="<b>1400</b>">
<div class="pod_prod_4">
Контент 1400
</div>
</div>
<div class="cards" data-views="<b>1300</b>">
<div class="pod_prod_4">
Контент 1300
</div>
</div>
Answer the question
In order to leave comments, you need to log in
is that how it should be?
if so, then:
1. there should not be a tag in the data-views attribute <b>
2. window.onload
you have a typo
3. you need to call sortable like this -sortable('.cards', 'data-views')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question