I designed a page that consists of two sections, with boxes in each. The functionality I implemented allows users to click on a box in either section and move it to the other section. Initially, this feature works smoothly for the first movement. Theoretically, the user should be able to move the boxes back and forth between the sections at will.
Box HTML:
<div id="top-area">
<div class="top-box" id="blue-box"></div>
<div class="top-box" id="yellow-box"></div>
<div class="top-box" id="green-box"></div>
</div>
<hr/>
<div id="bottom-area">
<div class="bottom-box" id="red-box"></div>
<div class="bottom-box" id="gray-box"></div>
</div>
To move the boxes, I utilize jQuery.remove()
to remove them from one section and jQuery.append()
to add them to the other. However, I encountered an issue where the jQuery event handler I created to move the boxes back to their original position does not trigger.
jQuery/JavaScript:
$(".top-box").on('click', function ()
{
var item = $(this);
item.remove();
$(this).removeClass("top-box").addClass("bottom-box");
$("#bottom-area").append(item);
});
$(".bottom-box").on('click', function ()
{
var item = $(this);
item.remove();
$(this).removeClass("bottom-box").addClass("top-box");
$("#top-area").append(item);
});
I have confirmed that the classes are correctly added and removed for the jQuery selectors. I have even used $(document).on()
to manage the event. Why are the boxes not triggering the jQuery events after being moved once?
Please refer to the Fiddle: http://jsfiddle.net/r6tw9sgL/