Solved

jQuery: Which element was moved?

Posted on 2014-03-11
2
334 Views
Last Modified: 2014-03-11
In this example, items may be dragged however I do not know how the dragged item can be identified.  I want the id of the dragged element to be returned in an alert.
http://jsfiddle.net/f4fZP/1/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="http://draggabilly.desandro.com/draggabilly.pkgd.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
var items = document.querySelectorAll('.pins>div');
  for ( var i=0, len = items.length; i < len; i++ ) {
    var item = items[i];
    var draggie = new Draggabilly( item );
    draggie.on( 'dragEnd', onDragEnd );
  }
function onDragEnd( draggie ) {
  alert( 'ID of pin that was moved: ????');
  alert( 'X: ' + $(this)[0]['position']['x'] +'\nY: '+ $(this)[0]['position']['y']);
}
});
</script>
<style type="text/css">
html, body, .pins {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.pins>div {
position: absolute;
width: 20px;
height: 20px;
background: red;
}
</style>
</head>
<body>
 <div class="pins">
  <div id="pin1" style="left: 40px; top: 50px;"></div>
  <div id="pin2" style="left: 120px; top: 140px;"></div>
  <div id="pin3" style="left: 200px; top: 130px;"></div>
 </div></body>
</html> 

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39920393
Try this:
alert( 'ID of pin that was moved: ' + draggie.element.id);

Open in new window

0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39920408
You can try
draggie.element.id

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 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

26 Experts available now in Live!

Get 1:1 Help Now