how to add an element before an element in jquery code example
Example 1: append before jquery
$( "h2" ).insertBefore( $( ".container" ) );
Example 2: jquery append before
Consider the following HTML:
Html:
---------------
<h2>Greetings</h2>
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
You can create content and insert it into several elements at once:
jquery:
--------------
$( ".inner" ).prepend( "<p>Test</p>" );
Each <div class="inner"> element gets this new content:
Html Result:
<h2>Greetings</h2>
<div class="container">
<div class="inner">
<p>Test</p>
Hello
</div>
<div class="inner">
<p>Test</p>
Goodbye
</div>
</div>