Reputation: 1439
I'm trying to get an array with only elements that have the property of background-image, however, my code isn't working and i'm not sure what's wrong.
elements = document.getElementsByTagName("*")
[].filter.call elements, (el) =>
if el.currentStyle
return el.currentStyle['backgroundImage'] isnt 'none'
else if window.getComputedStyle
return document.defaultView.getComputedStyle(el,null).getPropertyValue('background-image') isnt 'none'
Upvotes: 0
Views: 336
Reputation: 1105
According to the javascript documentation,
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
So you need to store your filtered array before using it:
elements = document.getElementsByTagName("*")
filteredElements = [].filter.call elements, (el) =>
if el.currentStyle
return el.currentStyle['backgroundImage'] isnt 'none'
else if window.getComputedStyle
return document.defaultView.getComputedStyle(el,null).getPropertyValue('background-image') isnt 'none'
// filteredElements is your new array with the filtered elements.
Upvotes: 1