tags:

views:

27

answers:

3

I'm trying to set a dynamic width to ul.sections by checking how many li's exist and multiply that by the width of the li ($("ul.sections").width()), which in this case happens to be 910px.

I'm not sure how to return the current number of visible list items. Maybe something like this?

$('ul.sections').width($("ul.sections li").length() * $("ul.sections li").width());

Which obviously doesn't work yet. Any ideas?

+1  A: 

.length is a property not a method, so just use $("ul.sections li").length like this:

$('ul.sections').width($("ul.sections li").length * $("ul.sections").width());

If you have multiple sections, this would be the jQuery 1.4+ version:

$('ul.sections').width(function(i, w) { return $(this).find('li').length * w; });
Nick Craver
Thank you, this was correct. I should have left off the `()`. I thought I was far off with the rest of it, but I was pretty close! Finally getting the hang of slightly intermediate jQuery yessss :)
steve
+1  A: 

there is a :visible jQuery Selector

to get all visible <li>'s in ul.sections

$("ul.sections li:visible")

so now you can set your width

$("ul.sections").width($("ul.sections li:visible").length * $("ul.sections").width()));
John Hartsock
I forgot to add the :visible selector before, thanks for reminding me.
steve
+1  A: 

use size()

http://api.jquery.com/size/

動靜能量
Both `size()` and `.length` return the proper numeral. Thanks.
steve