Reputation: 381
I can't get the 'width' attribute of an element:
console.log(document.querySelector('.container').width)
<div class="container" width="55">
</div>
This returns "undefined", other attributes like "id" I can get this way, but not width and height. I find this problem so silly, why doesn't it work? Even here they write it should work: https://www.w3schools.com/jsref/prop_object_width.asp
I don't need any workarounds like with jQuery or outerWidth, etc. I just need to get the width and height attributes from the element.
Edit: My actual problem is with SVG element, but with DIV it also doesn't work.
Upvotes: 0
Views: 613
Reputation: 9959
You are looking for Element.getAttribute()
(Documentation)
console.log(document.querySelector('.container').getAttribute('width'))
<div class="container" width="55"></div>
Upvotes: 4