tags:

views:

84

answers:

2

I have html structured like so.

<div id='container'>
  <div class='item'> ... </div>
  <div class='item'> ... </div>
  <div class='item'> ... </div>
  ...
  <div id='item_final'><input type="button" id='addOne'>...</div>
</div>

and what I am trying to do it add another item to the container class before the item_final div. The items are dynamic, so the number of them is unkown.

+4  A: 

Try the .insertBefore method.

$('<div>').insertBefore('#item_final');
David
`.` should be `#`
Felix Kling
@Felix: thanks, corrected.
David
To whoever down-voted this: The answer is perfectly correct.
Felix Kling
+3  A: 
$('#container div:last').before( $('<div>') );
Stefan Kendall
I personally like your approach, if you always want to refer to the "last" instead of a component that's "supposed" to be the last, then this way of doing it is more secure.
StudiousJoseph