JavaScript retrieving element's CSS values -
i'm making basic drag , drop system , need retrieve top
, left
properties of element being moved. if this:
var mover = document.getelementbyid('mover'); alert(mover.style.top);
will alert nothing ( ' ' )
is there way of retrieving css values (in js) without having define them js first?
you need use getcomputedstyle if wish retrieve properties computed rather defined.
https://developer.mozilla.org/en-us/docs/web/api/window/getcomputedstyle
from mdn link...
<script> function getthestyle(){ var elem = document.getelementbyid("elem-container"); var thecssprop = window.getcomputedstyle(elem,null).getpropertyvalue("height"); document.getelementbyid("output").innerhtml = thecssprop; } getthestyle(); </script>