How to use jQuery to get the combined width of the DIV in the container and pass this value to another element?

I hope to use jQuery to get the full width of all DIVs in the container and set this width for the body.

I guess I need something influenced

$('#container > div').width().[mulitply by quantity of DIVs in container].[set this result to body width]

Unfortunately, just typing the lines of what I want is not good enough! Can someone point me in the right direction?

Any help appreciated!

+3
source share
1 answer

It might look like this:

var overall_width = 0;

$('#container > div').each(function(index, elem) {
    var $elem = $(elem);
    overall_width += $elem.outerWidth() + parseInt($elem.css('margin-left'), 10) + parseInt($elem.css('margin-right'), 10);
});

$(document.body).width(overall_width);

This would add the width of the children div nodealong with the field (if any) and finally set the width from document.body.

+7
source

Source: https://habr.com/ru/post/1795517/


All Articles