Solved

jQuery: Apply new Draggabilly() to ALL matching elements

Posted on 2014-03-10
2
1,025 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Numbers in JQuery file 11 66
Setting up JqueryUI with MVC 5 in Visual Studio 5 23
How to resize a div in html 3 37
jQuery force form POST 7 45
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

839 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