tags:

views:

47

answers:

3
<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?

+6  A: 

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>');
Nick Craver
Beat me to it...
Rocket
Pretty sure I googled this for 10minutes and never found .before or .after. I fail. Thank you kind sir.
wish_i_was_nerdy
@wish: no need to Google! [jQuery's API docs](http://api.jquery.com) are _really_ good; always check those out first.
Matt Ball
A: 

Try $.before():

jQuery('.four').before('<div class="addme">Add Me!</div>');
Matt Ball
A: 
$('.three').after('<div class="addme">Add Me!</div>');
Rocket