Re: Draggable Problem With Dynamically Created Divs
I am creating a chat application where div's are created through innerHTML dynamically.
I want all these div's to be draggable , so i used the method .draggable to make them draggable.
But this draggable property is not being applied to all the div's but only to the last div created.
For example when div1 is created, it gets the draggable property, but when div2 is created div1
gets disabled i.e it looses its draggable property and is no more draggable,
but div2 is draggable , similarly when div3 is created div1 and div2 loose their draggable property
and only div3 remains draggable and so on.
I have created a raw example code:
- <html>
- <head>
- <script type="text/javascript" language="javascipt" src='http://localhost/jm/modules/mod_freichat/jquery/js/jquery.ui.core.min.js'></script>
- <script type="text/javascript" language="javascipt" src='http://localhost/jm/modules/mod_freichat/jquery/js/jquery.min.js'></script>
- <script type="text/javascript" language="javascipt" src='http://localhost/jm/modules/mod_freichat/jquery/js/jquery-ui.min.js'></script>
- <script type="text/javascript" language="javascipt" src='http://localhost/jm/modules/mod_freichat/jquery/js/jquery.ui.draggable.min.js'></script>
- </head>
- <title>
- Testing Draggables
- </title>
- <body>
- <div id="drag">This is a test page for testing multiple draggable divs</div>
- <div onmousedown="click()">CLICK ME TO CREATE DRAGGABLES</div>
- </body>
- </html>
- <script>
- id=1;
- function click()
- {
- var str='<div id="dragx'+id+'">Draggable_'+id+'</div>';
- $jn("#drag").html($jn("#drag").html()+str);
- $jn("#dragx"+id).draggable();
- id++;
- }
- </script>
Can anyone please tell me why is this happening,
is it because i am creating the div's through innerHTML
I would greatly appreciate any solutions to this problem
ThankYou