Skip to main content
added 3 characters in body
Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

     var a = 1, b = 2, c = 3;
    
  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main = ('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

     var a = 1, b = 2, c = 3;
    
  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

     var a = 1, b = 2, c = 3;
    
  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main = ('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});
added 36 characters in body
Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

     var a = 1, b = 2, c = 3;
    
  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

     var a = 1, b = 2, c = 3;
    
  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});
Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24

Some suggestions:

  • Use var when declaring variables, unless you really want to make global variables (which you probably don't)

  • Combine var statements using ,

  • Combine classes in toggleClass

     $(this).toggleClass('minus expand')
    
  • Cache selectors that are used often (like $('#main'))

  • You don't need to use $li2 inside the click handler, you can just use $(this)

  • Turn repeated code into a function

$(function(){
    var $main('#main'),
    slide = function(ele, find){
        return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
    },
    $li = $('> li.expand', $main).click(function(){
        var $ul = slide(this, 'ul.sub-level');
        $('> li > ul', $main).not($ul).slideUp('slow');
        $('> li.minus', $main).not(this).toggleClass('minus expand')
    }),
    $li2 = $('.sub-level > li.expand').click(function(){
       slide(this, 'ul.sub-level2');
    });
});