0

I have a draggable div(using jqueryui plugin for this) inside a table. I am able to drag a single div and keep it contained inside <td id=” middle-side”></td>. Now I am having issues trying to create additional divs that will be also be draggable and contained inside the <td id=” middle-side”></td>. The button I am using to append this new div is not reacting to the click. How would I be able to add new dragagble divs to <td id=” middle-side”></td>? JSFIDDLE

Html

<td class="middle-side">
                Keep me inside here!
                    <div class="draggable" class="ui-widget-content">
                        <p><b>Drag me around</b></p>
                    </div>

            </td>

Jquery

//To Drag
$(function() {
     $( ".draggable" ).draggable({ containment: "parent" });
});


//To Add New Div
var i = 0;
var remove = true; // added this 
$('#button').click(function(e) {
    $('<div/>').attr({
        'id' : i
    }).addClass('draggale').css({
        'top' : e.pageY - 20,
        'left' : e.pageX - 20
    }).appendTo('.middle-side');
    i++;
});

$('.middle-side').on('click','.draggable',function (){ // corrected spelling
    if(remove){
        $(this).remove();
    } else {
        //just to see if it was clicked
        $(this).css({'background-color': 'red'});
    }
});

5 Answers 5

1

You could store the div as a template in your .click function:

$('#button').click(function(e) {
    $('<div class="draggable" class="ui-widget-content"><p><b>Drag me around</b></p></div>').draggable({ containment: "parent" }).appendTo('.middle-side');
});

Updated fiddle: http://jsfiddle.net/0wbnud4k/3/

Sign up to request clarification or add additional context in comments.

Comments

1

You have two errors in your code:

  1. You misspelled draggable
  2. You didn't re-bind all your .draggable divs

Here's the new code:

$('<div/>').attr({
        'id' : i
    }).addClass('draggable').css({ // Change CSS class
        'top' : e.pageY - 20,
        'left' : e.pageX - 20
    }).appendTo('.middle-side');
// Re-bind all your draggable divs
$( ".draggable" ).draggable({ containment: "parent" });

See Fidddle: http://jsfiddle.net/thejsj/0wbnud4k/7/

Comments

0

try this:

$('#button').click(function(){
  $('.middle-side').append('<div class="draggable ui-widget-content"><p>text</p></div');
})

Comments

0

You misspelled draggable in one place, and also you have to re-initialize the draggable content after you add more of them.

Fiddle

$(function() {
     $( ".draggable" ).draggable({ containment: "parent" });
});

var i = 0;
var remove = true; // added this 

$('#button').click(function(e) {
    $('<div/>').attr({
        'id' : i
    }).addClass('draggable').css({
        'top' : e.pageY - 20,
        'left' : e.pageX - 20
    }).appendTo('.middle-side');
    i++;
    $( ".draggable" ).draggable({ containment: "parent" });
});

$('.middle-side').on('click','.draggable',function (){ // corrected spelling
    if(remove){
        $(this).remove();
    } else {
        //just to see if it was clicked
        $(this).css({'background-color': 'red'});
    }
});

Comments

0

First you have a typo in .addClass('draggale'), it should be .addClass('draggable')

Second you also have to call .draggable() on the new DIV to make it draggable

Here is an updated fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.