create array from htmlcollection 1

create array from htmlcollection

//Use the Array.from() method to convert a nodelist or HTMLcollection into an array.

let elements = document.getElementsByClassName("classnameHere");
let arrayOfElements = Array.from(elements);

//Now arrayOfElements is iterable with methods such as .forEach().

Here is what the above code is Doing:
1. We get a nodelist of elements with the class “classnameHere”.
2. We use the Array.from() method to convert the nodelist into an array.
3. We assign the array to the variable arrayOfElements.
4. We can now use array methods on arrayOfElements.

Similar Posts