JQuery The width of an element depending on its parent width

I have a pixel size table whose cell 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 their parents (). The idea is that the width of this input should be calculated automatically and set in pixels using jQuery. There are no classes or identifiers for these cells and inputs. I can set the width for a single cell only with 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 someone help me with this? Thanks!

+7
source share
1 answer

You can 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 line and provide them with the width of their parent.

+13
source

All Articles