tags:

views:

42

answers:

2

Hi all,

I am trying to wrap individual li tags with a nested ul. For example I want the first HTML snippet, to be the second:

Snippet #1

          <ul class="lof-main-wrapper">
            <li class="featured">
              <h3><a href="#">Title</a></h3>
              <a href><img src="" /></a>
              <p class="kicker"></p>
              <p class="summary"></p>
            </li>

            <li class="featured">
              <h3><a href="#">Title</a></h3>
              <a href><img src="" /></a>
              <p class="kicker"></p>
              <p class="summary"></p>
            </li>
           </ul>

Snippet #2

          <ul class="lof-main-wrapper">
            <ul>
             <li class="featured">
               <h3><a href="#">Title</a></h3>
               <a href><img src="" /></a>
               <p class="kicker"></p>
               <p class="summary"></p>
             </li>
            </ul>

            <ul>
             <li class="featured">
               <h3><a href="#">Title</a></h3>
               <a href><img src="" /></a>
               <p class="kicker"></p>
               <p class="summary"></p>
             </li>
            </ul>
           </ul>

I am currently using the .wrap() function, which does what I want, but it duplicates the child elements into each nested ul, adding me more content to the DOM than what should be.

How do I prevent the duplication from occurring without painstakingly using the .remove() method to get rid of the duplicates?

FYI - I'm new to jQuery and JS in general, so any direction you can provide is appreciated.

Thanks,

Brion

+1  A: 

You shouldn't wrap it to where a <ul> is directly inside a <ul> since that's invalid markup.

However, you can wrap them validly in a <li> with a <ul> inside using .wrap() to wrap each element the selector finds, like this:

$("#lof-main-wrapper li").wrap("<li><ul></ul></li>");
Nick Craver
@nick, that `/lu` should be `/ul`
Gaby
@Gaby - thanks :) the worst for me is: `<kbd></kbd>` but for some reason all closing tags are hard :'( VS spoils me, autoclosing and such
Nick Craver
@Nick, lol .. wrong kbd seems impossible to notice.., and btw the `lof-...` is a class
Gaby
DUOH! I don't know what I was thinking!!! must...get...sleep.
Brion
Thanks everyone!So, here's another problem I'm having. I want to prepend an <h3> and <img>--which are in a nested <div>--to the parent <li>. I'm using the following, which has worked previously, but now is not.$('.lof-main-item-desc img').prependTo($('.lof-main-wrapper ul .featured'));$('.lof-main-item-desc h3').prependTo($('.lof-main-wrapper ul .featured'));<li class="featured"> <div class="lof-main-item-desc"> <p class="kicker">Making the Switch</p> <h3><a href="#">Surviving an SIS Breakup</a></h3> <img src='image.jpeg' /> <p class="summary"></p> </div> </li>
Brion
My apologies for the lack of formatting. The text field stripped what I added.
Brion
A: 

Use the wrap method

$('.lof-main-wrapper li').wrap('ul');

but as @Nick mentions in his answer, what you are trying to do is invalid, in HTML terms..

About the duplication you mention, you would have to show us the jQuery code you use..

Gaby