Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within HTML Pages with CSS and JavaScript
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
HTML Pages with CSS and JavaScript
how to get and set the position of an element in Java Script
sandeep Kumar
Ranch Hand
Posts: 41
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello All,
I want the functionality of getting the position of an element and also setting the postion of an element in
Java
Script. If any body has some working code snippetts, Please help me.
Thanks in Advance.
Regards,
Sandeep.
Eric Pascarello
author
Posts: 15385
6
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
To position:
Well you have CSS with position absolute or relative with top and left. You also have margins and padding to move it.
To read position:
Either you can read the CSS if it is positioned absolutely.
If not you have to do something like this:
function getElementPosition(elem){ var posX = 0; var posY = 0; while(elem!= null){ posX += elem.offsetLeft; posY += elem.offsetTop; elem = elem.offsetParent; } return { x : posX, y : posY }; }
You would call it:
var myDiv = document.getElementById("myDivId"); var pos = getElementPosition(myDiv); alert( pos.x + "," + pos.y); myDiv = null;
Eric
Bear Bibeault
Sheriff
Posts: 67746
173
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
if this is really important, then I recommend you check out jQuery and its Dimensions plugin which will take care of all these calculations for you.
[
Asking smart questions
] [
About Bear
] [
Books by Bear
]
sandeep Kumar
Ranch Hand
Posts: 41
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Eric Pascarello and Bear Bibeault,
Thanks for your valuable suggestions.
Regards,
Sandeep.
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
place div element in lower left corner of browser using jQuery
Adding jquery to a page
javascript css issue
Textbox value removal
Auto complete in JSF
More...