In this simplified version of my code, I'm trying to extract the css width as a number without the percentage attached. Researching a solution, I found this: Get a number for a style value WITHOUT the “px;” suffix which solves the problem. However, on trying to reproduce the same result, I'm getting
barSize: NaN
instead of
barSize: 90;
Update: This question isn't asking how the get the property as in
document.querySelector('.defaultBar').style.width
which gives:
barSize: 90%
but how to get the property without the suffix as in
parseInt(document.querySelector('.defaultBar').style.width, 10);
which should give: barSize: 90
without the % suffix but gives NaN instead.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>
.defaultBar {
background-color: green;
height: 4px;
width: 90%;
}
</style>
</head>
<body>
<div class="defaultBar"></div>
<script>
const barSize = parseInt(document.querySelector('.defaultBar').style.width, 10);
console.log("barSize: ", barSize);
</script>
</body>
</html>