How to wrap various elements in a div tag with jQuery?
-
I have an html structure that looks like this: <h5>Title</h5> <p> Content </p> <ul> <li>Item</li> <li>Item</li> </ul> <p> Content </p> <h5>Title</h5> <p> Content </p> <ul> <li>Item</li> <li>Item</li> </ul> <p> Content </p> In summary it's just some headers with content below them, I just need everything below the header tags in a div like this: <h5>Title</h5> <div> <p> Content </p> <ul> <li>Item</li> <li>Item</li> </ul> <p> Content </p> </div> I've tried using the .wrap function of jQuery but no luck since there can be multiple types elements below the header tags, I found this question: http://stackoverflow.com/questions/2040008/jquery-wrap-sets-of-elements-in-div which is very similar but haven't been able to fit it into what I need, can anyone help me? Thanks in advance!
-
Answer:
You can try this: http://jsfiddle.net/GGjXN/1/ $(function() { $('h5').each(function(i, e) { $(e).nextUntil('h5').wrapAll('<div>'); }); });
javiervd at Stack Overflow Visit the source
Other answers
Do this: $(document).ready(function () { $('h5').each(function () { $(this).nextUntil('h5').wrapAll('<div class="box"></div>'); }) })
Kamyar
Try this var $div; $("h5").each(function(){ $div = $("<div />"); if($(this).nextAll("h5").length){ $(this).after($div.append($(this).nextUntil("h5"))); } else{ $(this).after($div.append($(this).siblings())); } });
ShankarSangoli
I would just stick a div in the page after the h5, and manually insert the elements into it. Assuming that everything is inside something, like the body tag (untested!): <body> <h5>Title</h5> <p> Content </p> <ul> <li>Item</li> <li>Item</li> </ul> <p> Content </p> </body> var children = $(document.body).children(), $child $div; for(var x = 0, child; child = children[x++];) { $child = $(child); if($child.is('h5')) { $div = $('<div></div>').after($child); } else { $child.appendTo($div); } }
Andy Ray
Related Q & A:
- How to remove div completely in jQuery?Best solution by Stack Overflow
- how to parse a xml file using jquery and phonegap?Best solution by Stack Overflow
- how to add a form with a unique id on jquery?Best solution by Stack Overflow
- How to wrap tight a cut finger a child is picking on?Best solution by Yahoo! Answers
- How can I get the font size of text in elements within a webpage?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.