Alex Rivera | Logout

jQuery: appendTo parent

Asked 2011-01-13T12:14:41.770
22

I can't seem to get the appendTo to work. What do I do wrong?

$('div:nth-child(2n) img').appendTo(parent);

Current markup:

<div class="container">
  <img src="123.jpg" />
  <p>Hey</p>
</div>
<div class="container">
  <img src="123.jpg" />
  <p>Hey</p>
</div>

I want this output:

<div class="container">
  <p>Hey</p>
  <img src="123.jpg" />
</div>
<div class="container">
  <p>Hey</p>
  <img src="123.jpg" />
</div>

Please help me guys... I'm tearing my hair of every minute.. :-S

Edit
Report

2 Answers

8

Is this what you're after?

$('.container > img').each(function () {
    $(this).parent().append(this);
});

It simply takes the <img> within every container and moves as the first child of the container.

answered 2011-01-13T12:19:50.047
1

I made a little example and I hope you mean the same thing...

$(document).ready(function() {
    $('.container > img').each(function() {
        $(this).parent().append(this);
    });
});
answered 2011-01-13T12:49:25.690

Your Answer