Swap an element with another element
Hello friends,
i am new to jquery and i am trying to swap or move an element with another element, but it's not working completely.
I have following html:
- <div id=content>
- <div class=c1>
- <a href="link1"><img src="" alt=""></a>
- <h2>Headline 1</h2>
- <p>...</p>
- <p>...</p>
- </div>
- <div class=c2>
- <a href="link2"><img src="" alt=""></a>
- <h2>Headline 2</h2>
- <p>...</p>
- <p>...</p>
- </div>
- <div class=c3>
- <a href="link3"><img src="" alt=""></a>
- <h2>Headline 3</h2>
- <p>...</p>
- <p>...</p>
- </div>
- </div>
And I want that it looks like following html when an if-condition gets true:
- <div id=content>
- <div class=c1>
- <h2>Headline 1</h2>
- <a href="link1"><img src="" alt=""></a>
- <p>...</p>
- <p>...</p>
- </div>
- <div class=c2>
- <h2>Headline 2</h2>
- <a href="link2"><img src="" alt=""></a>
- <p>...</p>
- <p>...</p>
- </div>
- <div class=c3>
- <h2>Headline 3</h2>
- <a href="link3"><img src="" alt=""></a>
- <p>...</p>
- <p>...</p>
- </div>
- </div>
I tried this
- function myFunc() {
- var picHTML = $('div#content > div.c1 > a').html();
- if (condition) {
- $('div#content > div.c1 > h2').after(picHTML);
- }
- }
...but it's not working like i want.