dorsaf dhouibi dorsaf dhouibi - 2 months ago 12
HTML Question

How do I force jQuery to not automatically close a tag?

I have a link and I want to create a

nav
tag before the link tag, then close it after the
a
tag.

code:

<a href="" id="test">link test</a>


I want the code will be like this:

<nav class="link-effect">
<a href="" id="test">link test</a>
</nav>


I tried the following
.before()
,
.after()
,
.append()
,
.prepend()
but the closing tag is created automatically.

Answer

Use .wrap function of jquery.

$( "#test" ).wrap( '<nav class="link-effect"></nav>' );

Please check below snippet.

$( "#test" ).wrap( '<nav class="link-effect"></nav>' );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="" id="test">link test</a>