jQuery code to get the x, y coordinate of mouse click of any visible element.

in

While trying to get the x and y coordinates of the location where a user clicked on a particular image, i just stumbled upon a "not-a-definite-solution" with posts written on different blogs and a lot of different techniques people have used.

So here is my version with bits and pieces put together.

First lets get the x,y coordinates of any given element on the page.

File: jquery.elementlocation.js

  1. jQuery.fn.elementlocation = function() {
  2. var curleft = 0;
  3. var curtop = 0;
  4.  
  5. var obj = this;
  6.  
  7. do {
  8. curleft += obj.attr('offsetLeft');
  9. curtop += obj.attr('offsetTop');
  10.  
  11. obj = obj.offsetParent();
  12. } while ( obj.attr('tagName') != 'BODY' );
  13.  
  14.  
  15. return ( {x:curleft, y:curtop} );
  16. };

Add above javascript file into any html page and we can do a

  1. location = $("element-selector").elementlocation();
  2. // access with location.x and location.y

Next, we use the pageX and pageY property of the eventObject to get the location in the webpage where any event occured. Then finally substract the x and y coordinate of image itself to get the relative position of the event.

  1. <html>
  2. <head>
  3. <script src="jquery-1.3.2.min.js"></script>
  4. <script src="jquery.elementlocation.js"></script>
  5. <script>
  6. $(document).ready( function() {
  7. $("#clickable-image").mousemove( function( eventObj ) {
  8.  
  9. var location = $("#clickable-image").elementlocation();
  10. var x = eventObj.pageX - location.x;
  11. var y = eventObj.pageY - location.y;
  12.  
  13. $("#x-coordinate").text( x );
  14. $("#y-coordinate").text( y );
  15. });
  16.  
  17. });
  18.  
  19. </script>
  20. </head>
  21. <body>
  22. <div>Click on the image to get the coordinate.</div>
  23. <div><img id="clickable-image" src="test-image.png"/></div>
  24. <p>X:<span id="x-coordinate"></span></p>
  25. <p>Y:<span id="y-coordinate"></span></p>
  26. </body>
  27. </html>

All the files are attached with the post.

Comments are welcome.

AttachmentSize
click-demo.html696 bytes
jquery.elementlocation.js.txt358 bytes
jquery-1.3.2.min_.js.txt55.91 KB
test-image.png180.56 KB

Comments

hi

thank for the post

great site.

gr8 site . keep it up .I found the above post very useful to me . thank you for posting.

I am able to get this script to work but it gives me different x,y coordinates in IE than in FF and Chrome. 

Thanks a lot. This helped me a lot.

Great post!

رائعة
لكنها لا تعمل

Great but doesn't working

The following works without the need for any additional plugin. It uses jQuery.offset() which was added in version 1.2.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  5. <title>Coordinates!</title>
  6. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
  7. <script>
  8. $(document).ready( function() {
  9. $("#surface").mousemove( function( e ) {
  10.  
  11. var offset = $(this).offset();
  12. var x = e.pageX - offset.left
  13. var y = e.pageY - offset.top;
  14.  
  15. $("#x-coordinate").text( x );
  16. $("#y-coordinate").text( y );
  17. });
  18. });
  19. </script>
  20. <style type="text/css" media="screen">
  21. body { margin: 50px; }
  22. #surface {
  23. background-color: #ccc;
  24. width: 400px;
  25. height: 300px;
  26. }
  27.  
  28. </style>
  29. </head>
  30. <body>
  31. <div id="surface">
  32. Hover me!
  33. </div>
  34. <p>
  35. X: <span id="x-coordinate"></span><br />
  36. Y: <span id="y-coordinate"></span>
  37. </p>
  38. </body>
  39. </html>

Just to confirm this works, I tested it using jQuery versions

  • 1.2 √
  • 1.3.2 √
  • 1.4.2 √

r u using this option.its totally waste..lol.

actually i need a code in Java.the concept is the rectangular box has to place next to the previous box by getting order while clicking he mouse.

on the second click the box have to place next to the first and third click next to tat as so on...

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Easily link to terms in various wikis. For help, see <a href="/interwiki/1">interwiki</a>.

More information about formatting options