ONYX
ONYX

Reputation: 5859

Determine all child elements width

Is there a method i can do to determine all child elements width in a single line of code. Adding up all child widths

Upvotes: 6

Views: 6399

Answers (2)

user113716
user113716

Reputation: 322622

If you only want children elements, do this:

Example: http://jsfiddle.net/PV2dR/

var t=0;
$('#parent > *').width(function(i,w){t+=w;});

Or you can do this with the same outcome:

var t=0;
$('#parent').children().width(function(i,w){t+=w;});

Upvotes: 5

Jacob Relkin
Jacob Relkin

Reputation: 163318

This?

var width = 0;
$('selector > *').each(function() { width += $(this).width(); }); 

jsFiddle example

Upvotes: 9

Related Questions