Using JQuery, I need to be able to press a button and create a draggable div. My code is as follows
I have the following style in the body
<style>
#draggable {
width: 100px;
height: 100px;
background: #ccc;
}
</style>
This is the container where I want the div to be placed and the button that should create the new div. When I create the div on page load, it is draggable and works as expected.
<container id="area">
<a class="btn btn-primary" id="create_block">Create</a>
<div id="draggable">Draggable</div>
</container>
I then have this script in the body that creates the draggable on button click and the code that, from what I understand, makes all things with id="draggable" a draggable.
<script>
$( "#create_block" ).click( function() {
$( '<div/>' ).attr({
'id' : 'draggable'
}).appendTo("#area");
});
$( '#draggable' ).draggable();
</script>
When I click the button, it creates a div that is styled correctly, but it is NOT draggable.
I'm not sure what's not working with this. I've looked at some different examples of similar things but it seems that this should work. What am I missing? I am using bootstrap, but no other frameworks.
How can I make a new div, and all others created by pressing the button, draggable?