pgarama
pgarama

Reputation: 80

give element same width as previous sibling

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.

Upvotes: 1

Views: 1636

Answers (2)

roryf
roryf

Reputation: 30170

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

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359966

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);
});

Upvotes: 4

Related Questions