I am implementing a jquery function which is able to insert a div in the right place depending on the existing dom structure
The case of existing dom structure are the following
<!-- case 1 -->
<div class='container'>
<div class='foo'></div>
<div class='bar'>last bar</div>
<div class='bar'>some bar</div>
<div class='bar'>first bar</div>
</div>
<!-- case 2 -->
<div class='container'>
<div class='bar'>last bar</div>
<div class='bar'>some bar</div>
<div class='bar'>first bar</div>
</div>
<!-- case 3 -->
<div class='container'>
</div>
In every 3 cases, it should add a new <div class='bar'></div>
inside container but before the first <div class='bar'></div>
here is the code which works for the case 1 and 2 but not for the 3.
var last_bar = $('<div>').addClass('bar').text('last bar');
$($('.container').find('.bar')[0]).prepend(last_bar);
</p>
Any hints how to make this code more general?
here is the demo http://jsfiddle.net/3wFda/