Filter is not a function?

The querySelectorAll does not return an array, that is the reason why filter is not defined (which is a function of array). In this case, you should call filter function from Array.prototype and use textContent.indexOf(string) to check if the element has the text content you want (Flexbox). For sample:

var items = document.querySelectorAll('li');
var filter   = Array.prototype.filter;

var itemsFlex = filter.call(items, function(item) {
    return item.textContent.indexOf('Flexbox') >= 0;
});

console.log(itemsFlex);

See the sample here: https://jsbin.com/hibixipoyo/edit?html,js,output


querySelectorAll returns a NodeList not an Array. You can convert it to an Array if you'd like to use Array methods.

var items = document.querySelectorAll('li');
var itemsArray = Array.from(items);

Tags:

Javascript