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

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

How do I get a computed style?

See this answer. It’s not jQuery but, in Firefox, Opera and Safari you can use window.getComputedStyle(element) to get the computed styles for an element and in IE you can use element.currentStyle. The returned objects are different in each case, and I’m not sure how well either work with elements and styles created using Javascript, but … Read more

tech