I don't know if the title is very clear but I have an HTML page which has a div on it that is using the JQuery draggable feature.
It works as I expect but if I try to add the same element using JavaScript it is not draggable like the one that is added statically.
I have my HTML page
<!DOCTYPE html>
<html>
<!-- JQUERY LIBRARIES -->
<script src="assets/jquery/jquery-1.12.0.min.js"></script>
<script src="assets/jquery/jquery-ui-1.11.4/jquery-ui.js"></script>
<!-- CUSTOM -->
<script src="scripts/main.js"></script>
<!-- STYLE -->
<style>
.object { background-color:#333333 ; width:100px ; height:60px ; }
</style>
<body>
<!-- DRAG WORKS ON THIS DIV -->
<div class="object ui-widget-content"></div>
<!-- CREATE ANOTHER ONE -->
<button onclick="newObject()">NEW OBJECT</button>
</body>
</html>
Inside my main.js I have
$(document).ready(function() {
// APPLY JQUERY DRAGGABLE TO CLASS
$(".object").draggable();
});
function newObject() {
// CREATE A DIV LIKE THE ONE THAT EXISTS
var object = document.createElement("div");
// CREATE CLASS LIKE THE DIV HAS FOR STYLE
var styleClass = "object";
// CREATE SECOND CLASS FOR DRAGGABLE LIKE THE DIV HAS
var dragClass = " ui-widget-content";
// EXECUTE ABOVE
object.className += styleClass;
object.className += dragClass;
document.body.appendChild(object);
}
When the button is clicked it creates a div although the div it creates is not draggable like the one that already exists.
Why is this and is there a way around it?