1$('ElementBeforeYouWantToInsert').prepend('<div>the element you want to insert</div>');
1Consider the following HTML:
2
3Html:
4---------------
5<h2>Greetings</h2>
6<div class="container">
7 <div class="inner">Hello</div>
8 <div class="inner">Goodbye</div>
9</div>
10
11You can create content and insert it into several elements at once:
12
13jquery:
14--------------
15$( ".inner" ).prepend( "<p>Test</p>" );
16
17Each <div class="inner"> element gets this new content:
18
19Html Result:
20<h2>Greetings</h2>
21<div class="container">
22 <div class="inner">
23 <p>Test</p>
24 Hello
25 </div>
26 <div class="inner">
27 <p>Test</p>
28 Goodbye
29 </div>
30</div>