Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable jQuery draggable in child element

I am using jQuery draggable. I have added draggable function to main div. Now in all the child elements it's also draggable. How can I disable dragging inside child div if parent is draggable?

$(function() {
  $("#draggable").draggable();
});
#draggable {
  width: 150px;
  height: 150px;
  padding: 0.5em;
  border: black solid 2px;
}

.noDrag {
  width: 100px;
  height: 50px;
  border: blue solid 2px;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="draggable" class="ui-widget-content">
  <p>Drag me around</p>
  <div class='noDrag'>No Drag</div>
</div>
like image 772
Escoute Avatar asked Sep 15 '17 09:09

Escoute


1 Answers

To fix this use the cancel property, and provide it a selector to match the element you want to disable the drag behaviour on, like this:

$(function() {
  $("#draggable").draggable({
    cancel: '.noDrag'
  });
});
#draggable {
  width: 150px;
  height: 150px;
  padding: 0.5em;
  border: black solid 2px;
}

.noDrag {
  width: 100px;
  height: 50px;
  border: blue solid 2px;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="draggable" class="ui-widget-content">
  <p>Drag me around</p>
  <div class="noDrag">No Drag</div>
</div>
like image 118
Rory McCrossan Avatar answered Sep 24 '22 12:09

Rory McCrossan