I have some pair of element, they are
<h3></h3><ul></ul>
<h3></h3><ul></ul>
<h3></h3><ul></ul>
I want to wrap a element (div) with jquery around all of this pair
result should be
<div> <h3></h3><ul></ul> </div>
<div> <h3></h3><ul></ul> </div>
<div> <h3></h3><ul></ul> </div>
How can I achieve it with jquery.wrap()
?
You can iterate through h3
elements and using jquery .next() to select and the ul
element with .andSelf() to include and h3
element and finally .wrapAll() around a div
:
$('h3').each(function() {
$(this).next('ul').andSelf().wrapAll('<div/>');
});
div {
border: solid 1px red;
}
h3 {
border: solid 1px green;
}
ul {
border: solid 1px yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>h3</h3>
<ul>ul</ul>
<h3>h3</h3>
<ul>ul</ul>
<h3>h3</h3>
<ul>ul</ul>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments