One
Two
Three
Four

Add div over another div

<div>
  <div class="one">One</div>
  <div class="two">Two</div>
  <div class="three">Three</div>
  <div class="four">Four</div>
  <div class="five">Five</div>
</div>

I need to add a div between divs Three and Four, but I cannot use any targeting on the parent div (only divs inside it).

jQuery('.four').parent().prepend('<div class="addme">Add Me!</div>');

This, as you probably know, adds it to the top, above div One. Without ".parent ()", it adds the div inside the Four div, before the content. Same difference for ".append ()".

Has anyone got a solution?

+5
source share
2 answers

You can use .before()either .after()as follows:

jQuery('.four').before('<div class="addme">Add Me!</div>');
//or...
jQuery('.three').after('<div class="addme">Add Me!</div>');
+15
source
$('.three').after('<div class="addme">Add Me!</div>');
0
source

All Articles