jQuery UI draggable is not working on newly created DOM element
Solution 1
I know its been a while, but this problem recently bugged me too. As someone else mentioned, you got to rerun .draggable()
on the newly created item, but that doesn't work if you defined certain options in your .draggable()
. Also what didn't work was putting the $().draggable()
in a function and then calling the function after creating a new element (this trick does however work with resetting droppables - go figure).
Anyway, long story short -> Putting the $().draggable()
setup in a function and then calling the function after creating new element DID WORK, but i had to TAKE IT OUT of the document ready
function..... after i disabled that, it worked.
You can call that function as many times and it keeps on working after every created element. It seems if it's in the document.ready
statement then its a 1 shot deal..
Hope that helps.
Solution 2
You need to call draggable() after the newly created element is inserted into the DOM.
The reason being, is that the first line of code is only matching existing elements. Newly created elements aren't selected in that first line.
Solution 3
Actually it seems that there is problem with draggable. When you append some htmlTag and then try to find it and make it draggable, it do not recognise it. try to make a new element using createElement
and then append it. Hope it works
var newEle = document.createElement('p');
$(newEle).attr("class","draggable").draggable();
$('body').append($(newEle));
or
var newEle = document.createElement('p');
$(newEle).attr("class","draggable");
$('body').append($(newEle));
$('p.draggable').draggable();
Solution 4
You should call draggable() function everytime you call an event action:
$('body').on('click', '.add-new-table', function () {
$( ".canvas" ).prepend('<div class="ui-widget-content draggable"><p>Drag me</p></div>');
$(function(){
$(".draggable" ).draggable({
containment: "parent"
});
});
});
Vivek
Updated on June 04, 2022Comments
-
Vivek almost 2 years
I have some DOM element which is draggable using jQuery UI.All are working fine but when i create some element using jQuery , then they are not draggable at all. i.e
$('div.draggable').draggable(); //Existing element , it works :) $('p.draggable').draggable() ; //Newly created paragraph with same class name, it doesnt work at all :(
Thanks in advance !!!
I am trying This :
<script type="text/javascript"> $(document).ready(function(){ $('body').append('<p class="draggable">Newly Created Paragraph</p>'); $('p.draggable').draggable(); **//This is not working** }); </script>
However Somehow this is working
<script type="text/javascript"> $(document).ready(function(){ $('body').append('<p class="draggable">Newly Created Paragraph</p>') .find('p.draggable').draggable(); **This is working** }); </script>
-
Vivek about 12 yearsThanks for your effort, but i have tried that as well, i kept it inside
$(document).ready()
as well as also tried to write the<script></script>
at the end of document ie. just before</body>
tag as well, but it doesnt work :( -
Justice Erolin about 12 yearsI don't think you're doing it correctly. How is a newly created paragraph inserted in the HTML/DOM? After it is created, .draggable() needs to be called on that element.
-
Vivek about 12 yearsThanks for ur tym and effort , but still a try with no luck yet :(
-
Tejasva Dhyani about 12 yearsI think
$('p.draggable')
selects all the 'p' elements with class 'draggable' anddraggable()
once applied to an element does not apply again, rather the previously applied draggable also gets corrupted. Try.draggable().draggable()
on any element and then you can see. However, if you apply draggable before appending itself and then append, it will apply draggable() to only that element and that too once. -
Nico Haase over 5 yearsWhy use
setTimeout
for this? The other solutions, which are much older, work without it -
Tyler2P over 3 yearsHi and welcome to Stack Overflow! Please take the tour. Thanks for answering but can you also add an explanation on how your code solves the issue? Check the help center for info on how to format code.