-1

I have this script I'm working on. What it should do is generate a new div every time I press a button and I've already done that, but those are not draggable. I think that happens because of the onLoad function but alternative do I have? The second problem is that when I click one of the blue boxes, all of them must arrange in a line like you see them when you load my jsFiddle link, and if the line is full and you click the "New Square" button, instead of overflowing or add a new line, it would close the last box instead and add a new box before all of the other boxes.

I'm sorry to bother you with those questions, but I am a PHP and MySQL programmer, I just started using jQuery.

jsFiddle

HTML:

<div id="container">
    <div class="round"></div> 
    <div class="round"></div>
    <div class="round"></div>
    <div class="round"></div>
</div>
<br><br><br>
<input type="button" class="button" value="New Square">

jQuery:

$('.round').draggable();

$('.button').click(
    function (){
$("#container").append("<div class='round'></div>");
    });

$('.round').click (

    );

I had more code in jQuery, but I removed all of the useless options because I want you to easily understand what I want to do.

1 Answer 1

1

you have to assign the draggable class to the newly created object:

$('.button').click(
   function (){ 
    $("#container").append("<div class='round'></div>");
    $(".round").draggable();
});  

That should do it actually. Or Perhaps:

$("#container").append("<div class='round draggable'></div>");
2
  • $("#container").append("<div class='round draggable'></div>"); works. Thanks! Now I need to solve the other problem.
    – Sergiu
    Commented Aug 16, 2013 at 9:48
  • I will accept it, but it's still incomplete. I have 2 problems that I try to solve within this post. The other one is to arrange all of the blue boxes in a line when I click one of the boxes just like it it when you click my jsFiddle link, and if you click the "New Square" button and the line is full, delete the last of the boxes and add a new box before all of the boxes.
    – Sergiu
    Commented Aug 16, 2013 at 9:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.