When a list item is clicked, I want to clone it and display it in a separate div. If another list item is clicked, the previously displayed item should be replaced with the newly clicked one.
Below is my code along with a link to the codepen where you can view it: https://codepen.io/AnthonyDavid/pen/xxqmqQY
$('li').click(function() {
$(this).each(function(i) {
$("<div class='special'>")
.append($(this).contents().clone())
.appendTo('#container');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container"></div>
<ul>
<li><a href="">First</a></li>
<li><div>Second</div></li>
<li><span>third</span></li>
<li><h4>Fourth</h4></li>
</ul>