Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Jquery get X and Y coordfinates of an elemnet within a DIV

Hi

I have a container element of 980px. I want to find the x and y coordinates of an element within that container element.

Im using .position() .
This retrieves the coordinates in relation to the whole viewport. How can i get the coordinates in relation to the container eleemnt?

Thanks
0
coolispaul
Asked:
coolispaul
  • 2
1 Solution
 
Scott Fell, EE MVEDeveloperCommented:
I think you want offset http://api.jquery.com/offset/
0
 
coolispaulAuthor Commented:
hmm i tried that but where do you tell it watch element to offset it against?
0
 
Julian HansenCommented:
Works for me
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
	console.log ($('#fred').position());
});
</script>
<style type="text/css">
</style>
</head>
<body>
<div style="width: 200px; height: 200px; border: 1px solid green">
	<div style="left: 30px;top: 17px; position: absolute; width: 50px; height: 29px; background: blue" id="fred">
	</div>
</div>
</body>
</html>

Open in new window

0
 
Julian HansenCommented:
Can you give a bit more info on what the solution was here.
A B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now