Drag'n'drop for dynamically created elements (jQuery)?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
5 views
In jQuery there is a good plugin jQuery UI Draggable, which allows you to create effects drag'n'drop. It works quite simply: it is necessary for any existing item to invoke the method .draggable():
$('#test').draggable();

If you try to call this method to dynamically added element, nothing happens:
$('body').append('New item'); $('#newblock').draggable();


New item will not be able to drag'n'drop. How to make here something like the analogue of the function .live () to dynamically created elements also react to these methods?
by | 5 views

2 Answers

0 like 0 dislike
<!DOCTYPE html>\r\r\r\rjQuery UI Example Page\r\r\r\r$(function(){\r$('body').append('Новый элемент');\r$('#newblock').draggable();\r});\r\r\r\r:) \r\r\r

\r
Everything works fine. You would be better example.
by
0 like 0 dislike
Too lazy to try, try livequery (works when cloning — adds draggable) because when cloning is also creating an element dynamically and works, might work in your case.
\r
$(".drag") .livequery(function(){ $(this) .draggable({ helper: 'clone', opacity: 0.7, revert: 'invalid' }); }); 

examples: bit.ly/azEmUP
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 5, 2019 by JackShcherbakov
0 like 0 dislike
4 answers
110,608 questions
257,186 answers
0 comments
28,059 users