<div class="group">
<label>Text: </label>
<input type="text" />
</div>
<div class="group">
<label>Text: </label>
</div>
I want to wrap label + input
inside another div wrapper like this:
<div class="group">
<div class="wrapper">
<label>Text: </label>
<input type="text" />
</div>
</div>
<div class="group">
<label>Text: </label>
</div>
I tried with this:
$(".group").each(function() {
$(this).find("label, input").wrapAll('<div class="wrapper"></div>');
});
the problem is that even when is there only label
inside the div.group
is wrapped. I want to wrap label and input if they are on div.group, but if there is only input or label do not wrap.
One more alternative:
$(".group").each(function() {
var hasLabel = $(this).children("label").length;
var hasTxtInput = $(this).children("input[type='text']").length;
if (hasLabel && hasTxtInput)
$(this).find("label, input").wrapAll('<div class="wrapper"></div>');
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments