What's wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What's wrong with my code?

var target = document.getElementById("target"); var yt = target.getAttribute("top"); alert(yt); Returns null, but top is defined in the css of target as top: 50px; Is it a sintax problem or it is not the apropiate way to get the value? please help me

14th Jan 2017, 2:25 PM
C.E.
C.E. - avatar
5 Answers
+ 13
var yt = target.style.top;
14th Jan 2017, 2:27 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 6
Attributes are something like href in <a href="#">. They are not equivalent to styles in CSS.
14th Jan 2017, 2:31 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 6
Thank you both for helping me 😁
14th Jan 2017, 2:35 PM
C.E.
C.E. - avatar
+ 3
At the end I don't know why using var yt =target.style.top didn't work for what I needed (I wanted to compare between two elements) but I found .offsetTop worked fine thank you both 😊
14th Jan 2017, 9:42 PM
C.E.
C.E. - avatar
+ 2
You are good madam,congratulation i like you for your learning effort,
18th Jan 2017, 7:14 PM
basan
basan - avatar