tags:

views:

140

answers:

2

I have the following HTML structure:

<div id="subgroup">
    <h3>Group name #1</h3>
    <a href="#">Link #1</a>
    <a href="#">Link #2</a>
    <h3>Group name #2</h3>
    <a href="#">Link #3</a>
    <a href="#">Link #4</a>
</div>

I have this flat structure because I want to use jQuery UI's accordion effect. I want to wrap all a elements between the h3 elements.

I tried the following, without luck:

$('#subgroup a').nextUntil('h3').wrapAll('<div></div>');

But this caused some of the a elements to disappear. I've tried quite a few selectors but none of them has worked. Am I doing this right?

+2  A: 

An approach like this should work, though I'm guessing there are many different ways of doing this:

$('#subgroup h3').each(function() {
    $(this).nextUntil('h3').wrapAll('<div></div>');
});
Matt Gibson
That didn't work, (yeah I added `<div></div>` in `wrapAall()`. I'll try experimenting with this solution.
rebellion
Sorry, I quickly checked that it worked for me by adding `<div style='background-color: red'></div>` and then took too much out when I posted my answer! In what way doesn't it work? What does the HTML look like in Firebug after the jQuery's been run?
Matt Gibson
Check my answer below.
rebellion
A: 

This is what it looks like after some nice (and dirty) jQuerying, to tidy up the HTML:

<div id="subgr-productlist">

    <h3>Serie 1000</h3>
    <a id="product-PROD79" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD79" name="product-PROD79">Model 1010</a>
    <a id="product-PROD80" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD80" name="product-PROD80">Model 1020</a>
    <a id="product-PROD81" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD81" name="product-PROD81">Model 1030</a>
    <a id="product-PROD82" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD82" name="product-PROD82">Model 1040</a>
    <a id="product-PROD83" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD83" name="product-PROD83">Model 1050</a>

    <h3>Serie 2000</h3>
    <a id="product-PROD234" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD234" name="product-PROD234">Model 2010</a>

    <h3>Serie 3000</h3>
    <a id="product-PROD85" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD85" name="product-PROD85">Model 3010</a>

    <h3>Serie 4000</h3>
    <a id="product-PROD86" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD86" name="product-PROD86">Model 4010</a>
    <a id="product-PROD87" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD87" name="product-PROD87">Model 4020</a>
    <a id="product-PROD88" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD88" name="product-PROD88">Model 4030</a>
    <a id="product-PROD89" href="/global/hygiene/touchfree-dispensers/dispensers/products.aspx?ProductId=PROD89" name="product-PROD89">Model 4040</a>

</div>

In other words, all the other a elements disappear?

Then I run this jQuery script:

$('#subgr-productlist h3').each(function(){
    $(this).nextUntil('h3').wrapAll('<div class="test"></div>');
});

But that results in the #subgr-productlist div to render like this:

<div id="subgr-productlist">
    <h3>Serie 1000</h3>
    <h3>Serie 2000</h3>
    <h3>Serie 3000</h3>
    <div class="test">
        <a id="product-PROD85" href="/global/hygiene/touchfree-dispensers/dispensers/products/products.aspx?ProductId=%20Model%203010" name="product-PROD85"></a>
    </div>
    <h3>Serie 4000</h3>
</div>
rebellion
Odd. I just ran your code on your html, and got the right output. I can't see any reason any elements would disappear just running that code; wrapAll() should never remove anything, and that's the only active function running.I think something subtle or weird is going on somewhere. Do you have any extra javascript running anywhere? Can you reproduce the problem on a simple webpage with just the html and jQuery you've posted?
Matt Gibson
Hmm, I just got it to work myself, and I have NO idea why it wouldn't work earlier. Emptying cache, multiple browsers etc didn't work, but all of a sudden it just worked... I must have overlooked something. Thanks a bunch Matt! :D
rebellion
No problem! Sometimes you just need a sanity check :) Hope it carries on working for you.
Matt Gibson