There is a code

<ul> <li> <ul> <li>text</li> <li>text</li> <li>text</li> </ul> </li> <li> <ul> <li>text</li> <li>text</li> <li>text</li> </ul> </li> </ul> 

It is necessary to determine the height of the initial and when you click to set it. Now the code is

 height = "тут должна быть изначальная высота элемента"; $('ul > li > ul').css('height','0px'); $('ul > li').click(function() { $(this).children('ul').animate().css('height',height); }); 

How can I record the height of elements without creating different variables?

  • height = $('ul > li > ul').css('height') - Grundy
  • but li could be any number - Sdafs Fasafs
  • so what? :-) save the value for the top level for example - Grundy

2 answers 2

You can store the value of the initial height of the element in its data-attribute: https://jsfiddle.net/vkhLeL6m/

 $(document).ready(function($){ $('ul ul').each(function(){ var $el = $(this), height = $el.height(); $el.data('height', height); $el.height(0); }); $('ul>li').click(function(){ var $ul = $(this).children('ul'); $ul.animate({ 'height':$ul.data('height') }); }); }); 

    For example like this

     $(document).ready(function(){ var height = $('ul > li > ul').height(); $('ul > li > ul').height(0); $('ul > li').click(function() { $(this).children('ul').animate({ height: height }); }); }); 
    • Only it is not clear why you need to animate height, if you want to make a drop-down animated list, it is better to use slideUp and slideDown - Kirya
    • it works crookedly, your example for only one li will work - Sdafs Fasafs
    • Do you want to make a drop-down list? - Kirya
    • jsfiddle.net/5ow2zc0L/1 for perhaps this is what you need - Kirya