Sortable: (Sortable.create) is it works with class ?

Created on 16 Sep 2015  路  4Comments  路  Source: SortableJS/Sortable

var el = document.getElementsById('simpleList');
var sortable = Sortable.create(el);
works fine

var el = document.getElementsByClassName('simpleList');
var sortable = Sortable.create(el);
while using class it shows this error in console:

el.addEventListener is not a function

Most helpful comment

For those of you who did not find an answer, this is my code to create Sortable for all of the element using the same class:

const elementList = document.getElementsByClassName("subsection");

for (let i = 0; i < elementList.length; i++) {
    Sortable.create(elementList[i], {
        animation: 150
    });
}

HTMLCollection is a list of class element, isn't it? 馃拑
So yes, it could work with class.

All 4 comments

Because el must be HTMLElement, and not HTMLCollection.
See docs: https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName

Hi people,i don't know you have find a answer or not. in my case, i cut my script part and pasted it after my sortable list. now its working..

For those of you who did not find an answer, this is my code to create Sortable for all of the element using the same class:

const elementList = document.getElementsByClassName("subsection");

for (let i = 0; i < elementList.length; i++) {
    Sortable.create(elementList[i], {
        animation: 150
    });
}

HTMLCollection is a list of class element, isn't it? 馃拑
So yes, it could work with class.

For those of you who did not find an answer, this is my code to create Sortable for all of the element using the same class:
"
for (var i = 0; i < $('.subsection').length; i++) {
Sortable.create(document.getElementsByClassName("subsection")[i], {
animation: 150
});
}
". HTMLCollection is an collection array of class element, that's it 馃拑
So yes, it could work with class.

This works fine for me. Thank you

Was this page helpful?
0 / 5 - 0 ratings