Reputation: 71
How can I apply width of the li
to the a
? This should happen on resize.
So the a
always have the li
width.
var parentWidth = $('ul li').parent().width();
var $window = $(window);
$window.resize(function resize() {
$('ul li').each(function () {
if ($(this).closest('a').length == 1) {
parentWidth = $(this).closest('a').css('width').replace('px', '');
}
});
}
Upvotes: 0
Views: 1254
Reputation: 2403
as dragonslovetaco's has stated, you really should use CSS for this - check your updated fiddle:
Applying position:relative to the list items + having width:inherit on the a's will cause them to copy width.
a{background:red;display:inline-block; width:inherit;}
li{position:relative; width:100%;}
Upvotes: 1
Reputation: 38102
You can do:
var $window = $(window);
$window.resize(function() {
$('ul li').each(function () {
var parentWidth = $(this).width();
if ($(this).find('a').length) {
$(this).find('a').width(parentWidth)
}
});
});
Upvotes: 3
Reputation: 4484
If your anchor tags have a css property of "block" that should make the anchor element take the width of it's parent container. In this case, it is the "li" - which by default, is a block-level element.
If you are floating your list elements, give your "li" elements a width (using css). Then like you have now you can use "inline-block" to give your anchor tags a width (and/or height).
Upvotes: 1