Norman
Norman

Reputation: 6365

Move a div between two containers works only once

In my code, I'm trying to move the div from container_two to container when I click the span Add New. It happens once, but it wont work the second time. I need to use two separate <div>s due to changes that need to be made to the div that is moved. I guess this has to do with the parent part. Can you see how this can be made to work?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Home</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $('.add').click(function() {
    var $move = $('#container_two .move');
    $(this).parent("div").after($move);
    $(".move").show();
    }); 
 });

</script>
</head>
<body>

<div id="container">
    <div class="h1" data-id="1">Heading One<span class="add" data-id="US01">Add New</span></div>
    <div class="h2" data-id="2">Sub One <span class="add" data-id="US02">Add New</span></div>
        <br>
    <div class="h1" data-id="8">Head Two <span class="add" data-id="US10">Add New</span></div>
    <div class="h2" data-id="9">Sub Two <span class="add" data-id="US20">Add New</span></div>
</div>

<div id="container_two">
    <div class="move" style="display:none">Mock Form</div>
</div>

</body>
</html>

Upvotes: 0

Views: 160

Answers (1)

Itay
Itay

Reputation: 16777

If I got you right, you're missing the cloning of this item.

jsFiddle Demo

$('.add').click(function() {
    var $move = $('#container_two .move').clone();
    $(this).parent("div").after($move.show());
}); 

Upvotes: 1

Related Questions