views:

96

answers:

2

Using jQuery I want to give a specific element the same width (or min-width in this case) as the element before (sibling). In my case there is an unordered list (UL), which I want to give the same width as the div before. For example:

<div style="width:300px;"></div>
<ul class="list"><li>item1</li><li>item2</li></ul>
<div style="width:200px;"></div>
<ul class="list"><li>item1</li><li>item2</li></ul>

So the width of the UL should depend on the previous sibling and not on any other element.

+1  A: 

Does

$('ul').each(function()
{
    var width = $(this).prev().width();
    $(this).width(width);
});

do it? If you want to do it by getting/setting CSS instead:

$('ul').each(function()
{
    var width = $(this).prev().css('width');
    $(this).css('width', width);
});
Matt Ball
+1  A: 
$('.list').each(function() {
    var width = $(this).prev().width();
    $(this).width(width);
}):
roryf