Ideal would be a CSS solution. jquery is an option though.
There is a selection elts
of jquery elements. They have just been created (let elt = $('<div ..>some content</div>')
) and not yet been rendered.
What I would like to achieve, is to determine the maximum width among these
elements and set it to every single one of them. The usual CSS jazz of
width: n%_of_some_common_parent;
or width: some_fixed_value em
is not an
option in my case.
Finding no CSS answer, I tried JavaScript. However, being yet unrendered, the following code fails:
let max_width = 0;
elts.each(function() { max_width = Math.max(max_width, $(this).width()); });
console.log(`Maximum width: ${max_width}`);
elts.each(function() { $(this).width(max_width); } ); // << Fail, due to width == 0px.
Yields one big 0
.
What actually works, is using a timeout:
setTimeout(function()
{
elts = $('._common_cls');
query_common_maximum_width_and_set_it_to_all(elts);
}, 500); // << 500ms suffice. Edit: Actually 0 does, too. See below.
Nice, jumpy, fragile solution. However, what I really want is either
elts.on('render', function() { i_am_called_after_all_new_elements_are_rendered(..); })
or, best,- CSS:
.common_cls { width: calc('max_known_width_in_group')px; }
How to achieve that?
Edit - JavaScript solution.
At least in JavaScript the setTimeout
approach is a valid solution. Turns out setTimeout(.., 0)
is only evaluated after the current function/rendering stack has completed. This ensures that the timeout callback is triggered only after width > 0
is available. Thx to CBroe in the comments below.