jsldnppl
jsldnppl

Reputation: 913

jQuery - Avoid code repetition and shorten script

I've got the following line of jquery that targets each category item within my menu and moves the li to the first ul.main_navigation_level_2. The following works but I have to copy and paste it out 10 times for each different category.

$('li#main_navigation_dept_home_and_furniture_furniture ul.main_navigation_level_2')
  .children('li')
  .appendTo('li#main_navigation_dept_home_and_furniture_furniture ul.main_navigation_level_2:first');

The below is my attempt:

$(".main_navigation_level_2").each(function() {
    var menuItem = $(this);
    menuItem.children('li').appendTo(':first-child',this);
});

Upvotes: 0

Views: 109

Answers (1)

john Smith
john Smith

Reputation: 17906

this should be it

$(".main_navigation_level_2").each(function() {
    var menuItem = $(this);
    menuItem.children('li').appendTo(menuItem.first());
});

Upvotes: 2

Related Questions