Search code examples
jqueryappendappendto

appendTo not working as expected


I have a simple UL with all LI's having class="event".

The following jQuery is not working as expected.

$('#calendar-feed li.event').each(function() {
    $(this).find('h3').appendTo('h4');
});

I'd like to take the only h3 in every LI and append it to the only h4 in that LI.

Currently it is taking all the H3s and cloning them into each H4.

Is my syntax just wrong or am I thinking about this the wrong way?


Solution

  • Do this, because the appendTo is a new selector which can contain anything within the page. So you were selecting all H4 within the page and added them to the LI h3.

    $('#calendar-feed li.event').each(function() {
        $(this).find('h3').appendTo($(this).find('h4'));
    });