Plugin with multiple CSS selectors

Plugin with multiple CSS selectors

Hi!

I made this little plugin to make elements grow to match there container size.
It looks like this:

  1. (function($){
  2. $.fn.spaceWidths = function(options) {
  3. var parent_width = $(this).parent().width();
  4. var childs = 0;
  5. var child_count = $(this).size();

  6. this.each(function(){
  7. childs = childs + $(this).width();
  8. });
  9. var difference = parent_width - childs;
  10. var add_padding = Math.floor(difference / child_count / 2);
  11. return this.each(function(){
  12. $(this).css({
  13. 'padding-right': add_padding,
  14. 'padding-left': add_padding
  15. });
  16. });
  17. };
  18. })(jQuery);
This works great, when I'm only using it for 1 element on a page.

Let's say I've got the following:

  1. $("ul.list li").spaceWidths();
  1. <ul class="list">
  2.       <li>Hello</li>
  3.       <li>There</li>
  4. </ul>
The plugin now counts 2 childs.
This works, but If I add another list:

  1. $("ul.list li").spaceWidths();
  1. <ul class="list">
  2.       <li>Hello</li>
  3.       <li>There</li>
  4. </ul>
  1. <ul class="list">
  2.       <li>This will</li>
  3.       <li>not work</li>
  4. </ul>
The plugin now counts 4 childs.
This is incorrect, because it should do this for selected element separately. 

Anyone who can help me with this =)

Thanks!