How to wrap some items with jQuery?
I have this code which I can't directly modify:
<div id="registerMessage">
<div class="pageTitle" style="padding:0px 0px 5px 0px;">
Register
</div>
Some bla bla bla
<开发者_JAVA技巧/div>
<input type="hidden" name="ardir" value="" />
<input name="action" type="hidden" value="insert" />
<label>Email </label>
<div>
<input name="user[email]" id="user[email]" type="text" value="" size="35" class="inputReg" />
</div>
<label>Password (5 chars)</label>
<div>
<input name="user[passwd]" type="password" value="" size="35" class="inputReg" />
</div>
<label>Password (confirm)</label>
<div>
<input name="user[password_confirm]" type="password" value="" size="35" class="inputReg" />
</div>
<div id="memomessage">
(Other bla bla bal)
</div>
I need to wrap from the first label to the last div
(memomessage excluded) into a new div
:
<div id="registerBox"> the code of before </div>
$('label:first').nextAll().andSelf().not('#memomessage').wrapAll('<div id="registerBox">')
Here's a fiddle
use following
$('label:first').prev().nextUntil('#memomessage').wrapAll('<div id="registerBox"></div>');
http://jsfiddle.net/eeusN/1/
Assuming you don't care specifically about #memomessage
and want to get label and div pairs, you can use the next-adjacent selector:
var newDiv = $('<div />', {id:'registerBox'});
$('label + div').prev().andSelf().wrapAll(newDiv);
Working example: http://jsfiddle.net/x6pSZ/
精彩评论