Search code examples
jqueryhtmlcssclonenested-lists

Find active links parent and clone nested ul


//This works! but I was trying to avoid the extra class  
$("a.active").parent().addClass("active");
$('li.active ul').clone().appendTo('.leftnav').removeClass();

// this does not work
$("a.active").parent(function () {
    $('this ul').clone().appendTo('.leftnav').removeClass();
})

// does not work either
var activeLink = $("a.active").parent();
$(activeLink('ul')).clone().appendTo('.leftnav').removeClass();

Can you add to the "this" by looking for its nest ul?

if not

what about placing it in a variable?


Solution

  • here is the solution I found:

    $('ul.nav a.active').parent().children('ul').clone().appendTo('.leftnav').removeClass();
    
    • I found the active a
    • Then found its parent which was an li
    • Then I looked in the li's children and found the ul
    • I was then able to clone that ul and append it to the leftnav div