Home > Enterprise >  Get CSS font-weight value using Javascript style.fontWeight.value (getting undefined, expecting a va
Get CSS font-weight value using Javascript style.fontWeight.value (getting undefined, expecting a va

Time:10-08

I am aware that the value could sometimes be a word like "bold" but for this i am only using numbers and was expecting the output to show "900", instead i'm getting "undefined".

document.getElementById("results").innerText = document.getElementById("getfontweightfrom").style.fontWeight.value;
<div id="getfontweightfrom" style="font-weight:900;">
Test
</div>
<hr>
Result: <div id="results"></div>

CodePudding user response:

No need to add .value.

document.getElementById("results").innerText = document.getElementById("getfontweightfrom").style.fontWeight;
<div id="getfontweightfrom" style="font-weight:900;">
Test
</div>
<hr>
Result: <div id="results"></div>

CodePudding user response:

I think what you want is window.getComputedStyle

const weight = window.getComputedStyle(elem).fontWeight

CodePudding user response:

You don't need value property, just style.fontWeight

CodePudding user response:

You may use getComputedStyle()

elem = document.getElementById('getfontweightfrom');
style = window.getComputedStyle(elem);
weight = style.fontWeight;
alert(style.fontWeight)
<div id="getfontweightfrom" style="font-weight:900;">Test<div>

  • Related