简体   繁体   中英

Wrap two HTML elements in DIV with jQuery

I am trying to wrap two elements around a div, but my code is not working as expected. Here is my HTML:

<div class="form-group requiredField">
  <label for="password" class="col-sm-3 control-label">Password</label>
  <input type="password" class="form-control" id="password" name="password">
  <span class="help">help text</span>
</div>

Here is what I want my HTML to look like:

<div class="form-group requiredField">
  <label for="password" class="col-sm-3 control-label">Password</label>
  <div class="col-sm-9">
    <input type="password" class="form-control" id="password" name="password">
    <span class="help">help text</span>
  </div>
</div>

As you can see, I am trying to wrap the input and span tags in a <div class="col-sm-9"> . I have tried using the following jQuery snippets but to no avail:

//This only wraps the span
$("#form-group input").next().wrap('<div class="col-sm-9">');

I have also tried this:

//This only wraps the input
$("#form-group input").wrap('<div class="col-sm-9">');

Note: most of the answers, aside from your own, will not work with multiple form-group s.

Your aim appears to wrap all child elements, except the label , into a child div. If so a simple approach is literally to exclude the label with :not and then wrapAll :

$('.form-group').each(function(){
    $(':not(label)', this).wrapAll('<div class="col-sm-9"></div>');
});

JSFiddle: http://jsfiddle.net/6by51ytL/1/

Note: should you ever have deeper nested children in your form-group , add the immediate child selector ( > ):

$('.form-group').each(function(){
    $('>:not(label)', this).wrapAll('<div class="col-sm-9"></div>');
});

You can select both the elements using multiple selector then

$(".form-group").find('input, span').wrapAll('<div class="col-sm-9">');
//class selector is used for `form-group`

 $(".form-group").find('input, span').wrapAll('<div class="col-sm-9">'); 
 <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css"> <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.css"> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.js"></script> <div class="form-group requiredField"> <label for="password" class="col-sm-3 control-label">Password</label> <input type="password" class="form-control" id="password" name="password" required="" style="width:50%;"> <span class="help">help text</span> </div> 

Try below code use

Use jQuery .nextAll() and .wrapAll()

HTML

<div class="form-group requiredField">
  <label for="password" class="col-sm-3 control-label">Password</label>
  <input type="password" class="form-control" id="password" name="password">
  <span class="help">help text</span>
</div>

jQuery

$('.control-label').nextAll().wrapAll('<div class="col-sm-9" />');

JSFIDDLE DEMO

I figured it out. This code works for me:

$('.form-group input').each(function () {
    $(this).next('span.help').andSelf().wrapAll('<div class="col-sm-9">');
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM