how to tell an element’s position in JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to tell an element’s position in JS

so for a game i need to know an element’s position in javascript. how is this done?

12th Jul 2018, 2:23 AM
privrax.
privrax. - avatar
2 Answers
+ 10
privrax. get that element into a variable like var a = document.getElementById("id"); var top = a.offsetTop; //for vertical position var left = a.offsetLeft; //for horizontal. Store each of them in a variable i stored them in top and left you can choose yours.
12th Jul 2018, 2:31 AM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 2
if your element is set with css display:fixed or display:absolute, you could check your element #obj position by var element = document.getElementById("obj"); x = parseInt(element.style.x, 10); y = parseInt(element.style.y, 10);
12th Jul 2018, 3:20 AM
Calviղ
Calviղ - avatar