Alexis Alexis - 9 months ago 75
jQuery Question

Surround an element using jQuery

I wish surround an

. I'm trying :

<a class="toto">TEST</a>

var test = $('.toto');

However it does not work. How can I do this?

Answer Source

Your current code doesn't work as you can only append whole elements to the DOM. The output of what you're trying would be this:

<a class="toto">TEST</a>

To do what you require, use wrap():

$('.toto').wrap('<h2 />'); // or .wrap('<h1 />');
<script src=""></script>
<a class="toto">TEST</a>