Fancy Theme
Fancy Theme

Reputation: 85

How to change the place of an element in another element by javascript or jQuery?

I want to change the place of an element in another element

So this is what i have:

<div class="post-cont">
 <div class="post-thumail">
 </div>
<article>
  <div class="post-home">
    <div class="post-info">
     <div class="post-cont">
     </div>
    </div>
  </div>
</article>

I want to change the place of the div with class post-cont before the div of class post-thumail

like this :

<div class="post-cont">
 <div class="post-cont">
 </div>
 <div class="post-thumail">
 </div>
<article>
  <div class="post-home">
    <div class="post-info">
    </div>
  </div>
</article>

I used the before() and append() jquery proprietaries and they don't work for me

Upvotes: 1

Views: 69

Answers (2)

Anand Singh
Anand Singh

Reputation: 2363

Use this code...

$('article .post-cont').insertBefore($('.post-thumail'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="post-cont">
 <div class="post-thumail">
   1st
 </div>
<article>
  <div class="post-home">
    <div class="post-info">
     <div class="post-cont">
       2nd
     </div>
    </div>
  </div>
</articale>

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use .insertBefore():

$('article .post-cont').insertBefore($('.post-thumail'));

Working Demo

Note: you have encorrect closing tag for article. it should be </article>. also make sure to close main div.

Upvotes: 4

Related Questions