The problem is that your itemsArr variable does not only contains "li" objects, so when you iterate over it, you end up trying to access to the "dataset" property on objects where it doesn't exist.

If you debug your javascript, you will see that you have 3 "li" classes, the list's length and 2 functions (item() and namedItem()) and because of that, you get that error after the third iteration.

As someone else already said, you first need to verify if the property exists, but the code that he provided you will fail because he's also checking the property id of dataset, which is an undefined value. This code should work for you: