Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery: Apply new Draggabilly() to ALL matching elements

Posted on 2014-03-10
2
Medium Priority
?
1,148 Views
Last Modified: 2014-03-11
In this example, the draggabilly functionality is only applied to the first matching element.  How can I get it to apply to ALL matching elements?

http://jsfiddle.net/f4fZP/

<!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 draggie = new Draggabilly( '.pins>div' );
  draggie.on( 'dragEnd', function( draggieInstance, event, pointer ) {
  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 style="left: 40px; top: 50px;"></div>
  <div style="left: 120px; top: 140px;"></div>
  <div style="left: 200px; top: 130px;"></div>
 </div>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39917990
According to their own examples you have to bind individually

$( 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( 'X: ' + $(this)[0]['position']['x'] +'\nY: '+ $(this)[0]['position']['y']);
}
});

Open in new window


I did try with a jQuery foreach but didn't work

p.s.
Excellent example of a workable sample!
0
 
LVL 16

Author Comment

by:hankknight
ID: 39920367
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

963 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