I'm trying to wrap div with specific html code but I'm having problem using jQuery's .before and .after because jQuery would automatically close div I added into .before.
This is my html:
<div class="navigation">
<a>first</a>
<a>second</a>
<a>third</a>
</div>
When I try using this code:
$('.navigation').before('<div class="newNavigation"><a>Prev</a>');
$('.navigation').after('<a>Next</a></div>');
He will automatically close .newNavigation div, which is what I don't want to happen because I'm trying to close that div in .after code. Generated HTML looks like this:
<div class="newNavigation">
<a>Prev</a>
</div>
<div class="navigation">
<a>first</a>
<a>second</a>
<a>third</a>
</div>
<a>Next</a>
And I'm trying to get this:
<div class="newNavigation">
<a>Prev</a>
<div class="navigation">
<a>first</a>
<a>second</a>
<a>third</a>
</div>
<a>Next</a>
</div>
To help you little bit with this I created jsFiddle where I unsuccessfully tried using .before and .after.
Thank you in advance!
Solution:
Although there was few good solutions I decided to go with @Andy-E suggestion because his code looks cleanest to me.
$(".navigation")
.wrap('<div class="newNavigation">')
.before("<a>Prev</a>")
.after("<a>Next</a>");
Thank you all for participating!