Wrap anchors in list tags in jQuery
I have a code like this:
<div id="gallery"> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> </div> and I want to rewrite it with jQuery to create:
<div id="gallery"> <ul id="carousel"> <li><a href="#">link</a></li> <li><a href="#">link</a></li> <li><a href="#">link</a></li> </ul> </div> What is the best way?
+4
4 answers
Example: http://jsfiddle.net/pB98T/
$('#gallery > a').wrapAll('<ul id="carousel">').wrap('<li>'); This wraps all the <a> elements with <ul id="carousel"> with .wrapAll() , then wraps them individually with <li> with .wrap() .
+4
$('#gallery a').each(function() { $(this).wrap('<li class="liclass"/>'); }); $('.liclass').wrapAll('<ul class="ulclass"/>'); you can check its functionality http://jsbin.com/iluxe4
0
user372551
source share