Registered User
Registered User

Reputation: 3699

How to use jQuery appendTo() to insert elements before the children

I have:

<div id='1'>
    <div id='2'><div>
</div>

If I do: $("<img/>").appendTo('#1');

The result is:

   <div id='1'>
            <div id='2'><div><img/>
    </div>

How to chage the query so that the result will be?:

<div id='1'>
        <img/><div id='2'><div>
</div>

Upvotes: 2

Views: 2008

Answers (1)

jmar777
jmar777

Reputation: 39649

Just use prependTo(). E.g.,

$("<img/>").prependTo('#1');

Upvotes: 5

Related Questions