In below example I want to rotate elements using insertBefore, I mean every time I click the last object should be moved to first using insertBefore.
Here is simple example,
$elems = $('.parent').find('div');
$('body').click(function() {
$elems = $elems;
$elems.eq(4).insertBefore($elems.eq(0))
console.log('clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
As you can see after clicking first time, every single time same elements gets referenced and inserted before. Is it possible to use reference dom with same object? As this is really simplified example of complex problem, and it would help a lot if I can use variable instead of using jQuery selector every time.