Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 517
ONYX Avatar asked Dec 26 '10 22:12

ONYX


People also ask

How can you tell how many children an element has?

To check if an HTML element has child nodes, you can use the hasChildNodes() method. This method returns true if the specified node has any child nodes, otherwise false . Whitespace and comments inside a node are also considered as text and comment nodes.

How do I get the div element of all children?

If You want to get list only children elements with id or class, avoiding elements without id/class, You can use document. getElementById('container'). querySelectorAll('[id],[class]'); ... querySelectorAll('[id],[class]') will "grab" only elements with id and/or class.

How do you get all the children of a node?

To get all child nodes of an element, you can use the childNodes property. This property returns a collection of a node's child nodes, as a NodeList object. By default, the nodes in the collection are sorted by their appearance in the source code.


1 Answers

This?

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

jsFiddle example

like image 102
Jacob Relkin Avatar answered Sep 26 '22 12:09

Jacob Relkin