Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add div above another div

Tags:

html

jquery

<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 div Three and Four, but I can't use any sort of targeting on the parent div (only the 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 the ".parent()" it adds the div inside of div Four, before the content. Same difference for ".append()".

Anyone got a solution?

like image 317
wish_i_was_nerdy Avatar asked Oct 08 '10 17:10

wish_i_was_nerdy


1 Answers

You can use .before() or .after() like this:

jQuery('.four').before('<div class="addme">Add Me!</div>');
//or...
jQuery('.three').after('<div class="addme">Add Me!</div>');
like image 175
Nick Craver Avatar answered Oct 11 '22 13:10

Nick Craver