Alex Rivera | Logout

jQuery - use wrap() to wrap multiple elements?

Asked 2010-08-13T09:32:06.057
45

How would I go about using wrap() to wrap multiple elements (with different classes) inside a <div>?

For example, on the form I'm working on there is a big list of checkbox inputs and labels in the form of:

<input>
<label>
<input>
<label>

etc

I'm wanting to wrap a <div> around the input and label, so the result would be:

<div>
  <input>
  <label>
</div>
<div>
  <input>
  <label>
</div>

Thanks!

Edit
Report

1 Answer

5
$('input+label').each(function(){
    $(this).prev().andSelf().wrapAll('<div>');
});​
answered 2010-08-13T09:42:40.827

Your Answer