I am working on the demo code below. How can I use jQuery in these ways?:
1- Wrap the p
only if it has not already wrapped with .check-wrap-sapn
and
2- Unwrap only .check-wrap-sapn
and not any other parent?
What is happening now jQuery wraps the p
element with .check-wrap-sapn
as long as users clicks on #wrap
and removes all parents of p
even if there is not any wrapper called .check-wrap-sapn
$("#wrap").on("click", function() {
$("p").wrap("<div class='check-wrap-sapn'></div>");
});
$("#unwrap").on("click", function() {
$("p").unwrap("<div class='check-wrap-sapn'></div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
<div class="well">
<p>This is for Wrapping</p>
</div>
</div>
<button class="btn btn-default" id="wrap">Wrap</button>
<button class="btn btn-default" id="unwrap">Un Wrap</button>