Set / Copy javascript computed style from one element to another

Update: As @icl7126 suggested, here is a shorter version for practically the same usage. good thing to remember that this code would not run on most/older browser if not pre-compiled. Original (ES 2017): function copyNodeStyle(sourceNode, targetNode) { const computedStyle = window.getComputedStyle(sourceNode); Array.from(computedStyle).forEach(key => targetNode.style.setProperty(key, computedStyle.getPropertyValue(key), computedStyle.getPropertyPriority(key))) } Precompiled (ES 5): function copyNodeStyle(sourceNode, targetNode) { var … Read more

Get computed font-family in JavaScript

Here goes a way to get the computed font-family with JavaScript from the DOM: let para = document.querySelector(‘p’); let compStyles = window.getComputedStyle(para); let computedFontFamily = compStyles.getPropertyValue(‘font-family’) // e.g. “Times New Roman” Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle