Omid Omid - 2 months ago 16
HTML Question

jquery move element to last child position

Let's say I have a

<div>
that contains some random elements, but there is one element I've created with this unique selector :
$('.myUniqueElement')
. I want to use
.insertAfter()
to move this element at last child position in my
<div>
is that possible ? what is the solution ?

This is my solution: http://jsfiddle.net/silverlight/RmB5K/3/ is this OK ??

HTML:

<div class='p'>
<div class='myUniqueElement'>unique</div>
<div>item</div>
<div>item</div>
<div>item</div>
</div>
<input type='button' id='b' value='Do'/>


Javascript:

$('#b').on('click',function(){
$('.myUniqueElement').insertAfter($('.p :last-child'))
});


CSS:

.myUniqueElement{color:red;}

Answer

Try

var mydiv = $('my-div-selector');
mydiv.find('.myUniqueElement').appendTo(mydiv)

or simple

$('my-div').append($('.myUniqueElement'))
Comments