Sorry for the stupid 101 question. I'm not sure why this is not working. I'm trying to iterate through the various margin properties that are returned using getComputedStyle()
Any help would be greatly appreciated thanks in advance - CES
const constContentContainerStyles = window.getComputedStyle(constHTMLContentContainer);
let tmpStylesArray = ["marginTop", "marginLeft", "marginBottom", "marginRight"];
let x = 0;
for (let i = 0; i < tmpStylesArray.length; i++) {
//This throws undefined error
// x = constContentContainerStyles.tmpStylesArray[i];
//This returns 0
x = constContentContainerStyles.getPropertyValue(tmpStylesArray[i]);
console.log(tmpStylesArray[i] + " - " + x);
// returns the correct value
console.log(constContentContainerStyles.marginTop);
};
getPropertyValueexpects hyphen-case names ("margin-top", etc., not"marginTop", etc.).But you can just use the style object directly via brackets notation:
That supports camelCase (
"marginTop") and hyphen-case ("margin-top"):