Yazid
Yazid

Reputation: 108

Jquery Exact Count of a li elemnt

I'm trying to create an element(ul) that the width can be changed depending on the number of element(li) contained in this (ul). I used jquery to get this done but i don't get the right result as i'm making an error and i don't how to fix it. here is my jquery code:

jQuery('ul.class').each(function(){
    var n = jQuery('ul.class li').length;
    console.log(n);
    jQuery(this).css('width',n*(jQuery('ul.class li').width() + 10));
    console.log(n*jQuery('ul.class li').width());
});

10 in this code is the border or each element.

The issue is in this part var n = jQuery('ul.class li').length and i don't know of to get the exact count of each li contained in a ul.

li has a width 150px

Upvotes: 0

Views: 159

Answers (1)

RGS
RGS

Reputation: 5211

jQuery('ul.class').each(function(){
   var n = jQuery(this).find('li').length;
   alert(n);
   jQuery(this).css('width',n*(jQuery(this).find('li').width() + 10));
   alert(n*jQuery(this).find('li').width());
});

Demo:

http://jsfiddle.net/84Bv6/1/

Upvotes: 2

Related Questions