Mitya Ustinov
Mitya Ustinov

Reputation: 903

jQuery. Element width depending on its parent width

I have a pixel-sized table which cells' width depends on the content. There are input elements in the first row of the table and I need them to have the same width as they parents (). The idea is that those inputs' width should be calculated automatically and set in pixels with jQuery. There are no classes nor id's for those cells and inputs. I'm able to set the width for one cell only using selectors with code like this:

var parentWidth = $(".foo").width();
$('td.foo input').width(parentWidth);

But as I said I need it to be more universal without using any classes etc. Can anyone help me with that? Thank you!

Upvotes: 7

Views: 15284

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76890

You could use a child selector like this

$('tr:first td > input').each(function(){
    $(this).width($(this).parent().width());
});

This code should select all td of the first row and give them the width of their parent.

Upvotes: 14

Related Questions