Search code examples
jquerywidthparenteach

Add dynamic width based on parent div


what i am trying to do is to extract the width of let's say: .ow_newsfeed_left (it has different widths) and then add that width to a child div. i'm trying using each but it will asign only the first value.

This is the code i got so far

$('.ow_newsfeed_left').each(function() {
    $(".ow_newsfeed_string").css("left", $( '.ow_newsfeed_left' ).width() + "px");
});

Any help would be much appreciated!


Solution

  • Each time through, you're asking to set the left style of all .ow_newsfeed_string elements to the width() of all .ow_newsfeed_left elements (which, yes, will be the first width property found).

    You need to trim the scope down:

    $('.ow_newsfeed_left').each(function() {
       $(".ow_newsfeed_string", this).css("left", $( this ).width() + "px");
    });
    

    Now we're setting the width on .ow_newsfeed_string elements within the element we're looking at right now (this), to the width of that specific element.