tags:

views:

48

answers:

2

I'm probably doing something wrong but I've tried all sorts of things and can't seem to get a collection of jQuery objects wrapped. The following just outputs the link HTML, unwrapped. Any ideas?

$.each(sitemapSections, function(i) {
  var $sitemapSection = $(sitemapSections[i]);
  var $primary = $sitemapSection.find('a[data-level="1"]').wrap('<h3></h3>');

  $dropdownSections[i].html($primary);
});

EDIT - here's the markup (cleaned up):

<li id="product-solutions"><a href="#link" class="alpha grid-6">Products &amp; Solutions</a>

  <div id="ps-dropdown" class="dropdown-menu grid-20">
    <div class="ps-dropdown-section">

    </div><!-- .ps-dropdown-section -->

    <div class="ps-dropdown-section">

    </div><!-- .ps-dropdown-section -->

    <div class="ps-dropdown-section">

    </div><!-- .ps-dropdown-section -->
  </div><!-- .dropdown-menu -->
</li>

UPDATE - I got it! The comments who mentioned parent() is what I was missing. Here's the final code:

$.each(sitemapSections, function(i) {
  var $sitemapSection = $(sitemapSections[i]);
  var $primary = $sitemapSection.find('a[data-level="1"]').wrap('<h3></h3>').parent();

  $dropdownSections[i].html($primary);
});
A: 

You need a string to pass into .html(), I think you're after this instead:

 $dropdownSections[i].empty().append($primary.parent());

This gets the .parent() (since you just wrapped it in one) and sets the contents to that.

Nick Craver
this didn't do anything. still the same :(
CoryDorning
@CoryDorning - Can you post the markup you're working with?
Nick Craver
@CoryDorning - Oh, duh, my fault, you need to use `.parent()` since you just wrapped it :)
Nick Craver
where does the wrapping occur?
CoryDorning
@CoryDorning - `.wrap('<h3></h3>')` doesn't return the `<h3>`, it returns the element that you wrapped in the `<h3>`, if you want to append the actual `<h3>`, toy need to call `.parent()` :)
Nick Craver
@Nick you lost me man...the code you have above doesn't wrap each element of $primary in an <h3>. That's what I need.
CoryDorning
@CoryDorning - The above above is meant to replace only your `$dropdownSections[i].html($primary);` line, not the other 2...
Nick Craver
A: 

It looks like you mean to use jQuery's html method to add html in the second to last line - unless $dropdownSections is an array with each element being a jQuery object. I think you might be after this:

$.each(sitemapSections, function(i) {
  var $sitemapSection = $(sitemapSections[i]);
  var $primary = $sitemapSection.find('[data-level="1"]').wrap('<h3></h3>');

  $dropdownSections.eq(i).html($primary.parent().html());
});

or more written another way:

$.each(sitemapSections, function(i) {
  $dropdownSections.eq(i).html(
    $(sitemapSections[i])
    .find('[data-level="1"]')
    .wrap('<h3></h3>')
    .parent()
    .html()
  );
});

if $dropdownSections is an array which has a jQuery object in each element:

$.each(sitemapSections, function(i) {
  $dropdownSections[i].html(
    $(sitemapSections[i])
    .find('[data-level="1"]')
    .wrap('<h3></h3>')
    .parent()
    .html()
  );
});
Andrew Wirick
This doesn't work, as it strips out the link. but to answer you question, yes this is a collection. So i need each anchor wrapped in an <h3>Also, it didn't wrap it in an <h3> either...
CoryDorning
you're right! that wrap didn't work. after wrapping you should be able to go to the parent element (which would be the <h3> tag at that point) to get the html.
Andrew Wirick