How to do this with jQuery:
There are 4 divs with IDs. When a div is clicked on, it's moved to another div (let's call it newDiv1), and the other 3 are moved to newDiv2.
I can only do that with 1 specific div, using detach
and appendTo
, but I don't know how to apply the rule to all of them.
HTML:
<div class="park1">
<div id="car1">Car 1</div>
<div id="car2">Car 2</div>
<div id="car3">Car 3</div>
<div id="car4">Car 4</div>
</div>
<div class="park2"></div>
<div class="park3"></div>
<div class="park4"></div>
JS:
// when click on car1, car1 will move to park2, the other 3 to park3
$("#car1").on("click", function() {
$("#car1").detach().appendTo(".park2");
$("#car2").detach().appendTo(".park3");
$("#car3").detach().appendTo(".park3");
$("#car4").detach().appendTo(".park3");
});
// when car 2 is already at park3, click on it, it moves to park4
$("#car2").on("click", function() {
$("#car2").detach().appendTo(".park4");
});
I need that happens when I click on any of the 4.