Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 695
Mitya Ustinov Avatar asked Jun 13 '11 12:06

Mitya Ustinov


People also ask

How to set width of element in jQuery?

jQuery width() Method The width() method sets or returns the width of the selected elements. When this method is used to return width, it returns the width of the FIRST matched element. When this method is used to set width, it sets the width of ALL matched elements.

How to get element width in jQuery?

Use jquery Width command with the jcrop boxWidth, create a div layer around the image and then find the width using the command! boxWidth: $("#cropwindow"). width(), this will give you the width of the responsive area!

How to get the height and width of element in jQuery?

jQuery width() and height() MethodsThe width() method sets or returns the width of an element (excludes padding, border and margin). The height() method sets or returns the height of an element (excludes padding, border and margin).

How does jQuery width work?

The jQuery width() method uses to set and get the width of an element. So when we call width() method on selector(can be element name, id or class) without any parameter or without passing any value, then it returns the width of that particular element.


1 Answers

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.

like image 65
Nicola Peluchetti Avatar answered Nov 24 '22 09:11

Nicola Peluchetti