A
A
Anton-Pluton2018-02-24 08:56:03
JavaScript
Anton-Pluton, 2018-02-24 08:56:03

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>

What parameters should be added to the button so that the data-views element is sorted from the smallest to the largest element.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Epifanov, 2018-02-24
@Anton-Pluton

is that how it should be?
if so, then:
1. there should not be a tag in the data-views attribute <b>
2. window.onloadyou 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 question

Ask a Question

731 491 924 answers to any question