Search code examples
javascriptjqueryhtmljquery-uijquery-ui-resizable

how to make new live / cloned element resizable with jquery ui?


i am creating html elements live and appending them to the window.

The issue im having is that i can't use jquery ui on them. I have tried the deffer method, but that doesn't work as well.

What i am trying to accomplish is to clone the last div, add it to the container and make it resizable

here's some code jsfiddle:

<div id="container" class="ui-widget-content">
     <h3 class="ui-widget-header">Containment</h3>

    <div id="" class="ui-state-active resizable">
         <h3 class="ui-widget-header">Resizable</h3>

    </div>
    <div id="" class="ui-state-active resizable">
         <h3 class="ui-widget-header">Resizable</h3>

    </div>
</div>
<button class="add">add</button>

#container {width: 600px;height: 300px;}
#container h3 {text-align: center;margin: 0;margin-bottom: 10px;}
.resizable {width: 100px;height: 100px;float: left;}
.resizable, #container {padding: 0.5em;}

var dfd = $.Deferred();
dfd.done(function () {
    var lastDiv = $('.resizable').last();
    var lastDivClone = lastDiv.clone();
    lastDivClone.appendTo('#container');
    return lastDivClone;
});

$('.add').on("click", function () {
    var x = dfd.resolve();
    x.resizable("enable");
});

$(".resizable").resizable({
    containment: "#container",
    grid: 50
});

any ideas?


Solution

  • Remove the resizable handle first.

    lastDivClone.appendTo('#container').find('.ui-resizable-handle').remove()
        .end().resizable({
            containment: "#container",
            grid: 50
        });
    

    http://jsfiddle.net/ExplosionPIlls/945p6/2/