+ 1

Get CSS value with JavaScript

How would I get the value of a CSS attribute on an element with vanilla JavaScript? I have tried: const x = document.getElementById("arrow") if (x.style.left = "100px") { console.log(“Hello”) } I have also tried replacing “100px” with 100, but it still cannot read the property of null. Here’s my HTML body: <div id="arrow"></div> Here’s the CSS: #arrow { width: 6px; height: 100px; left: 100px; position: absolute; } Thank you!

21st Sep 2021, 4:47 AM
<p id=“username”>The Cuber</p>
<p id=“username”>The Cuber</p> - avatar
2 Réponses
+ 2
i don't know why but while sending u answer it is showing something went wrong so i have made a code (with explanation ) u can using 1. cssText to get only inline style 2. getComputedStyle https://code.sololearn.com/W56FK2ekeorM
21st Sep 2021, 5:41 AM
Pariket Thakur
Pariket Thakur - avatar
21st Sep 2021, 4:59 AM
Ipang