Mike Tavish Mike Tavish - 1 year ago 63
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>


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

Answer Source

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download