A
A
Anton-Pluton2018-02-23 21:42:00
JavaScript
Anton-Pluton, 2018-02-23 21:42:00

How to run a script on button click?

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');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Kanyshev, 2018-02-23
@Anton-Pluton

Let's say we have a button with Id="LOL"

<button id="lol">
  LOL
</button>

Then for this function it will be
And correspondingly passed parameters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question