I have this simple bit of code:
var recipes = document.getElementsByClassName("recipes");
var recipesStyle = window.getComputedStyle(recipes, null);
It returns this error message:
Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
I have no idea what I'm doing wrong here. Can anyone help?
The error message is telling you exactly what's wrong:
recipes
isn't an element (it's a collection of elements).If you want the styles associated with the first recipe element, either add
[0]
:...use
querySelector
, which will just return the first element matching a given CSS selector:Or if you wanted to deal with the styles of each recipe element, you might use a loop: