Solved

jQuery: Apply new Draggabilly() to ALL matching elements

Posted on 2014-03-10
2
988 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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

746 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

12 Experts available now in Live!

Get 1:1 Help Now