Mike Tavish Mike Tavish - 29 days ago 8
Javascript Question

Jquery: How add new parent to element

I trying to add new parent to child. i use this code

$("#span").before('<div class="newPaernt">');


this add div

<div class="newParent"></div><span id="span"></span>


not

<div class="newParent><span id="span"></span>

Answer

The purpose of before() method is inserting content before an element. For wrapping with an element use wrap() method.

$("#span").wrap('<div class="newPaernt">');

$("#span").wrap('<div class="newPaernt">');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="span">a</span>

Comments