Evan Ward
Evan Ward

Reputation: 1439

coffeescript - Array filtering not working

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

Answers (1)

Aliou
Aliou

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

Related Questions