jQuery: Apply new Draggabilly() to ALL matching elements

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?


<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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']);
<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;
 <div class="pins">
  <div style="left: 40px; top: 50px;"></div>
  <div style="left: 120px; top: 140px;"></div>
  <div style="left: 200px; top: 130px;"></div>

Open in new window

LVL 16
Who is Participating?
GaryConnect With a Mentor Commented:
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

Excellent example of a workable sample!
hankknightAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.