0

I'd like to break apart a list by inserting closing tags with jquery.

Here's the original:

<ul>
<li>one</l1>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>

and i'd like to turn it into:

<ul>
<li>one</l1>
<li>two</li>
</ul>
<ul>
<li>three</li>
<li>four</li>
</ul>

I haven't been able to accomplish this using :nth-child(n) or .html() or .append(), but I'm thinking perhaps some sort of outter html replace would do it. Maybe there's an easier way. Any ideas?

7
  • 1
    Are you looking for a way to break a list of 4 items into two lists of two items. Or something more generic? Commented Aug 9, 2011 at 22:22
  • Why are you performing basic content formatting with jQuery? You realize that anyone who is browsing on a platform without Javascript would have a degraded experence? Commented Aug 9, 2011 at 22:23
  • 3
    possible duplicate of jQuery split long ul list in smaller lists Commented Aug 9, 2011 at 22:23
  • 3
    @Devin, why are you assuming you know better or even know the poster's intentions? Maybe he's just experimenting with jQuery and wants to learn. Commented Aug 9, 2011 at 22:25
  • Im assuming that this would produce a bad expierence for someone who is not using Javascript. If the intention is to learn then thats fine. However it should be noted that this would be a task better applied to getting the markup correct in the first place. Commented Aug 9, 2011 at 22:28

3 Answers 3

1

$('li:nth-child(3), li:nth-child(4)').insertAfter('ul').wrapAll('')You can't "break a list". You can either A) remove it and replace it with two lists B) extract some elements to make another list with. The following is terrible terrible practice, but then again so is what you're asking...

$('li:nth-child(3), li:nth-child(4)').insertAfter('ul').wrapAll('<ul />')

http://jsfiddle.net/aJWCs/

Sign up to request clarification or add additional context in comments.

1 Comment

I agree with you in regards to those limitations. Your solution is essentially a correct one.
0

If you do the following, you can check the index of the object, and then do something if it is odd/even/modulus/etx

$('div').each(function(idx){
    console.log($(this));
    console.log(idx);
    console.log("");
});

Notice on the .each switch, you can pass it the idx (index) of the element. So.. if your selector selected 4 elements, you can do whatever math you want. Example:

$('ul li').each(function(idx){
    if(idx%2==1){
        $(this).after('</ul><ul>');
    }    
});

That would add a after every other one.

1 Comment

This is what I was doing initially, but some browsers were actually considering it an html error and fixing it by inverting the tags. Thanks for that though
0

This works JSFiddle

var items = $('ul li');
var list = $('ul');
var secondList = "<ul>";
if(items.length > 0) {
    var index = parseInt(items.length / 2)  ;
    for(var i=index; i<items.length; i++){
        secondList += "<li>" + items[i].innerHTML + "</li>";
        $(items[i]).remove();
    }
    secondList += "</ul>";
    $(secondList).insertAfter(list);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.