Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3943588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:48:00+00:00 2026-05-20T00:48:00+00:00

I’m trying to get the x and y of the draggable object using jQuery.

  • 0

I’m trying to get the x and y of the draggable object using jQuery.

The scenario is, I’m dragging and dropping an object onto another object and want to get the position of the drag & drop object.

Edit: Now I can get the position of the object but I need more:

Here is what I’ve tried:

<script>
$(function() {
    $('#draggable').draggable({
        drag: function() {
            var offset = $(this).offset();
            var xPos = offset.left;
            var yPos = offset.top;
            $(this).text('x: ' + xPos + 'y: ' + yPos);
        }
    });

    $("#droppable").droppable({
        drop: function(event, ui) {
            $(this)
                .addClass("ui-state-highlight")
                .find("p")
                .html("Dropped!");
        }
    });
});
</script>

Now I can get the position of the draggable object but I need it to be resizable and in addition to getting x,y of the draggable, I also need the size of it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:48:00+00:00Added an answer on May 20, 2026 at 12:48 am

    You can use the drag event:

    $('#dragThis').draggable({
        drag: function() {
            var offset = $(this).offset();
            var xPos = offset.left;
            var yPos = offset.top;
            $('#posX').text('x: ' + xPos);
            $('#posY').text('y: ' + yPos);
        }
    });
    

    JS Fiddle demo.

    This demo brought to you by the drag event, and the methods offset() and text().


    Edited in response to comment from OP, to original question:

    David, actually what I need is a bit more complicated but you might do great help. What I want to do is to put a draggable object and a droppable image in a panel and drag the draggable object on the image and get the position of where it was dropped. I’ll also need the draggable object to be resizable and get the size of it at the end too 🙂 thank you

    …uh, whoah…

    I think this sort of covers the basics of what’s asked for:

    $('#dragThis').draggable( {
        containment: $('body'),
        drag: function() {
            var offset = $(this).offset();
            var xPos = offset.left;
            var yPos = offset.top;
            $('#posX').text('x: ' + xPos);
            $('#posY').text('y: ' + yPos);
        },
        stop: function() {
            var finalOffset = $(this).offset();
            var finalxPos = finalOffset.left;
            var finalyPos = finalOffset.top;
    
            $('#finalX').text('Final X: ' + finalxPos);
            $('#finalY').text('Final X: ' + finalyPos);
        },
        revert: 'invalid'
    });
    
    $('#dropHere').droppable({
        accept: '#dragThis',
        over: function() {
            $(this).animate({
                'border-width': '5px',
                'border-color': '#0f0'
            }, 500);
            $('#dragThis').draggable('option', 'containment', $(this));
        }
    });
    

    Updated JS Fiddle demo.

    Newly-updated JS Fiddle demo, featuring revert: invalid, so dropping the draggable div anywhere but the droppable div will cause the draggable to animate back to its starting position. If that’d be appreciated. Or desired at all…

    To address the requirement for the draggable object to be resizable as well, I don’t think that this is possible, since both draggable() and resizable() respond to the same interaction. It may be possible, in some way, but it’s currently beyond my ken, I’m afraid.


    Edited to add in the ‘height/width’ requirement, and also to tidy up a few things and improve the CSS. That said:

    HTML:

    <div id="dragThis">
        <ul>
            <li id="posX">x: <span></span></li>
            <li id="posY">y: <span></span></li>
            <li id="finalX">Final X: <span></span></li>
            <li id="finalY">Final Y: <span></span></li>
            <li id="width">Width: <span></span></li>
            <li id="height">Height: <span></span></li>
        </ul>
    </div>
    
    <div id="dropHere"></div>
    

    CSS:

    #dragThis {
        width: 8em;
        height: 8em;
        padding: 0.5em;
        border: 3px solid #ccc;
        border-radius: 0 1em 1em 1em;
        background-color: #fff;
        background-color: rgba(255, 255, 255, 0.5);
    }
    
    #dragThis span {
        float: right;
    }
    
    #dragThis span:after {
        content: "px";
    }
    
    li {
        clear: both;
        border-bottom: 1px solid #ccc;
        line-height: 1.2em;
    }
    
    #dropHere {
        width: 12em;
        height: 12em;
        padding: 0.5em;
        border: 3px solid #f90;
        border-radius: 1em;
        margin: 0 auto;
    }
    

    jQuery:

    $('#dragThis').draggable({
        containment: $('body'),
        drag: function() {
            var offset = $(this).offset();
            var xPos = offset.left;
            var yPos = offset.top;
            $('#posX > span').text(xPos);
            $('#posY > span').text(yPos);
        },
        stop: function() {
            var finalOffset = $(this).offset();
            var finalxPos = finalOffset.left;
            var finalyPos = finalOffset.top;
    
            $('#finalX > span').text(finalxPos);
            $('#finalY > span').text(finalyPos);
            $('#width > span').text($(this).width());
            $('#height > span').text($(this).height());
        },
        revert: 'invalid'
    });
    
    $('#dropHere').droppable({
        accept: '#dragThis',
        over: function() {
            $(this).animate({
                'border-width': '5px',
                'border-color': '#0f0'
            }, 500);
            $('#dragThis').draggable('option', 'containment', $(this));
        }
    });
    

    JS Fiddle demo, of the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.