There is a div
with id parentDiv
, and there are some div with class block1
in parentDiv
divl
like
<div id="btn"><input name="click" type="button" value="Click" class='button" /></div>
<div id="parentDiv">
<div class="block1" style=" width:100px; height:100px; background:orange;">I am Block1</div>
<div class="block1" style=" width:100px; height:100px; background:orange;">I am Block1</div>
</div>
and In JQuery, div with class block1
, are draggable.
$(function(){
$('.block1').draggable({
drag: function() {
$('.block1').text('Drag Now!');
},
stop: function() {
$('.block1').text('Stop Now!');
}
});
});
These div are working as aspect, but the problem is, if any new div with block1
is appended in the parentDiv
by clicking on btn input like
$('#btn').on('click', 'input.button', function(){
var $newDiv=$('<div class="block1" style=" width:100px; height:100px; background:green;">I am Block1</div>');
});
that is not draggable.
Yes, It will not work, because it was not in DOM.
We are able to define a click
event on #btn
div to its children input.button
, and if we add new input with class button in this #btn
div, all will work as aspect.
So my question is, Is there a way to make all div draggable with in a parent container parentDiv
, like we can do with #btn
div?
You can use the jQuery on method with the mouseover
event to bind uninitialized draggable children:
$(".parentDiv").on("mouseover", ".block1", function() {
// Use the UI pseudo-selector to check that
// it is not already draggable
if(!$(this).is(":ui-draggable"))
$(this).draggable({
/* Options */
});
});
For convenience, wrapped in a function that extends jQuery:
$.fn.extend({
/**
* Children of the element with the given selector
* will automatically be made draggable
* @param {String} selector
* The selector of the child / descendant elements
* to automatically be made draggable
* @param {Object} opts
* The options that would normally be passed to the
* draggable method
* @returns
* The jQuery array for chaining
*/
draggableChildren: function(selector, opts) {
// On using event delegation to automatically
// handle new child events
$(this).on("mouseover", selector, function() {
// Check that draggable not already initialized
if(!$(this).is(":ui-draggable"))
// Initialize draggable
$(this).draggable(opts);
});
// Return this for chaining
return this;
}
});
You can use this as follows:
$(".parentDiv").draggableChildren(".block1", {
drag: function() {
$(this).text('Drag Now!');
},
stop: function() {
$(this).text('Stop Now!');
}
});
Here is a fiddle showing it in action
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With