jquery clone div and append it after specific div
try this out
$("div[id^='car']:last").after($('#car2').clone());
You can use clone, and then since each div has a class of car_well you can use insertAfter to insert after the last div.
$("#car2").clone().insertAfter("div.car_well:last");
You can do it using clone()
function of jQuery, Accepted answer is ok but i am providing alternative to it, you can use append()
, but it works only if you can change html slightly as below:
$(document).ready(function(){
$('#clone_btn').click(function(){
$("#car_parent").append($("#car2").clone());
});
});
.car-well{
border:1px solid #ccc;
text-align: center;
margin: 5px;
padding:3px;
font-weight:bold;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="car_parent">
<div id="car1" class="car-well">Normal div</div>
<div id="car2" class="car-well" style="background-color:lightpink;color:blue">Clone div</div>
<div id="car3" class="car-well">Normal div</div>
<div id="car4" class="car-well">Normal div</div>
<div id="car5" class="car-well">Normal div</div>
</div>
<button type="button" id="clone_btn" class="btn btn-primary">Clone</button>
</body>
</html>