Wrap items only if they are two certain elements

Mustapha Aoussar
<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.

jyrkim

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>');

});

Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Overflow only certain elements

HMENU wrap only elements with children

How can I place two items next to each other and make both of them wrap only when necessary?

Only display certain items in AngularJS

wrap all elements between two elements

Filtering a list. Get elements of list only with a certain distance between items?

Make flex items wrap two at a time

Always wrap at least two items with flexbox

How to wrap two elements in jQuery by respecting their parent?

How to wrap two dynamically created elements with span?

Is there a way to dynamically wrap two elements into a div IF they exist?

loop only deleting two items?

How to place only certain items in a flexbox to the right?

Show only certain items in legend Python Matplotlib

Display only certain Shopify line items

Only get certain items in TFS Visual Studio

Display only certain list items in listview with javascript

SQL Select items with only certain attributes

SonataAdminBundle disable editing only on certain items

SQL- Finding Orders with only certain items

How to wrap all elements inside a div after a certain element? (JQuery)

prevent line wrap between certain flex child elements

SCSS/CSS Using :not selector for only certain elements

Initialize only certain elements of array in dzn file

AngularJS $sce only trust certain elements

Select full tree and only certain child elements

Filter elements with a regex, only if they are in a certain block

Array Only Prints Certain Elements and Not Others

PHP for each Loop return only certain elements