Solved

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

Posted on 2013-06-27
4
186 Views
Last Modified: 2013-07-17
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
Comment
Question by:coolispaul
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
I think you want offset http://api.jquery.com/offset/
0
 

Author Comment

by:coolispaul
Comment Utility
hmm i tried that but where do you tell it watch element to offset it against?
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now