I have the following code (jsfiddled). I want to use red SVG rectangle as a handle for draggable div element, but because the SVG element is not a child of div, it is not possible. The idea is to let the handle (red rect) fire drag event of div and when div is being dragged, the code updates position of red SVG rectangle. How can I get SVG rectangle to act as drag handle for the div (so that SVG element is not a child of div element)?
I have tested to make SVG a child of div and used SVG rect as handle, and this worked normally. When I dragged SVG rect, the div and SVG rect were moved while dragging. But the problem is that also SVG element itself is dragged. But I don't want that SVG element itself is moved while dragging, only rect and div.
EDIT: Some sort of triggering drag event of div when pressing mouse on svg rect comes to my mind as a possible solution, but not tested yet.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Draggable - Handles</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<style>
#draggable { position:absolute; opacity:0.5;
width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
#draggable p { cursor: move; }
#draggable {left:30px;top:0px;z-index:1}
</style>
<script>
$(function() {
$( "#draggable" ).draggable({ handle: "p" });
$( "div, p" ).disableSelection();
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p class="ui-widget-header">I can be dragged only by this handle</p>
</div>
<svg width="500" height="500" style="position:absolute;z-index:-1">
<rect x="0" y="0" width="100" height="100" fill="#FF8888"/>
</svg>
</body>
</html>