prk_001
prk_001

Reputation: 381

Can't get 'width' attribute of an element with JS

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

Answers (1)

DBS
DBS

Reputation: 9959

You are looking for Element.getAttribute() (Documentation)

console.log(document.querySelector('.container').getAttribute('width'))
<div class="container" width="55"></div>

Upvotes: 4

Related Questions